5

I often find that I need to figure out the command to execute a certain application, such as an Alarm Clock application or the System Monitor. However, many of the actual CLI commands to run these are not directly evident. What I usually do is just go to the Ubuntu Software Center, then search for the application's "common name" and hence find its package/command name. I would like to know if there is an easier or more efficient way of doing this.

I was wondering if there is a command similar to xprop, where clicking on an open (graphical) application would output its command in the terminal. In other words, I am interested in a graphical way of finding an application's command.

For example, if I run such a command and click on System Monitor, the terminal would output gnome-system-monitor, etc. Thanks.

PS. If a solution is not currently available, I would be interested in writing my own script with this functionality. I only know basic bash, however. I realize that should probably go under a separate question, but I'm just throwing it out there.

deox
  • 624
  • Possible dupe: http://askubuntu.com/questions/23272/is-there-a-way-to-determine-how-to-launch-any-program-from-the-command-line?rq=1 – Kevin Bowen Mar 08 '13 at 05:03
  • @maggotbrain Though this does answer my question, a graphical method would be optimal. But thanks! – deox Mar 08 '13 at 05:20
  • I thought as much. I held off on flagging it for closure. In that case, can you edit and clarify your question? – Kevin Bowen Mar 08 '13 at 05:25

3 Answers3

2

I know you prefer a graphical route but here's my effort as a learner of CLI stuff ...

To list names with commands:

grep '^Name=' /usr/share/applications/*.desktop > ~/Desktop/mgtl1.txt && grep -h '^Exec=' /usr/share/applications/*.desktop > ~/Desktop/mgtl2.txt && paste -d':' ~/Desktop/mgtl1.txt ~/Desktop/mgtl2.txt > ~/Desktop/mgtl3.txt && perl -p -i -e 's/\/usr\/share\/applications\///'g ~/Desktop/mgtl3.txt && perl -p -i.bak -e 's/\n/\n\n/'g mgtl3.txt && perl -p -i.bak -e 's/:/\n/'g mgtl3.txt  

will create four files on the desktop and mgtl3.txt will look like this in part:

apport-gtk-mime.desktop  
Name=Report a problem...  
Exec=/usr/share/apport/apport-gtk -c %f

audacity.desktop  
Name=Audacity  
Exec=audacity %F

blueman-manager.desktop  
Name=Bluetooth Manager  
Exec=blueman-manager

To find the command for any software
Launch the software. Then, run

COLUMNS=150 ps -ef > ~/Desktop/out.txt  

Examine the last few lines of out.txt for the relevant command.

  • A little complicated, but otherwise good answer. +1 – Seth Mar 08 '13 at 16:00
  • It got complicated because not all .desktop files have Name= first and Exec= second. Those made it difficult for me. –  Mar 08 '13 at 16:11
  • @Seth, see this post: http://ubuntuforums.org/showthread.php?t=2123078&p=12545661#post12545661 I asked someone to post a .desktop file that comes with the Ubuntu version of LibO. If you look at it, you'll see that the Exec= line comes first, unlike the order in many other .desktop files which have Name= first. Actually, the order is Exec, Name, Name Exec! Bit bizarre. And that's what I see in my version of LibO as well (3.6.2.2 direct from libreoffice.org). –  Mar 08 '13 at 16:25
0

using UNITY

Dash -> Search for Terminal

Dash -> More Apps -> 'See More Results'

Dash -> More Apps -> Accessories

using Terminal Keyboard Shortcut: Ctrl + Alt + T

read this http://www.ee.surrey.ac.uk/Teaching/Unix/unix1.html

Seth
  • 58,122
3bu1
  • 346
0

I wrote this (rather ugly) script to find the command to execute a running application by clicking on it. It uses xprop to get the "clicking interface" and to get information about the selected window:

#!/bin/bash

xprop > ./tmp

chk_desk=`grep -c "_NET_WM_DESKTOP_FILE(STRING)" ./tmp` #Checks whether application has a corresponding .desktop file. The '-c' counts the number of matches.

if [ $chk_desk -ne 0 ] ; then
    desk_line=`grep "_NET_WM_DESKTOP_FILE(STRING)" ./tmp` #Extract line containing .desktop path 
    desk_path=${desk_line/"_NET_WM_DESKTOP_FILE(STRING) = "/} #Extract only the .desktop path
    desk_path=${desk_path//\"/} #Removes quotes
    exe_line=`grep "Exec=" $desk_path | head -n1`
    exe=${exe_line/"Exec="/}
else #If .desktop doesn't exist, it uses the PID to find the executable
    pid_line=`grep "_NET_WM_PID(CARDINAL)" ./tmp` 
    pid=${pid_line/"_NET_WM_PID(CARDINAL) = "/}
    exe_path=`readlink -f /proc/$pid/exe`
    exe=${exe_path/\/usr\/bin\//}
fi

echo $exe

rm ./tmp

exit 0

In case you are wondering, I could not have used only the PID for matching since /proc/$pid/exe gives you the path of the "real" executable, or of the underlying service running the program. So if you used it on say 'geogebra', the script would output java instead. Or in the case of Firefox you would get the path /usr/lib/firefox/firefox, which I suppose is where the actual firefox executable exists, which means the /usr/bin/firefox points to it.

While firefox is not a big problem (you could use the "real" executable), for the java based applications the .desktop file is needed to get the command to run the actual application and not a blank java session.

This script, of course, will only work if you are using X as your display server (since it uses xprop). I'm sure there must be a way to make the script cleaner, though my main problem was having to create many variables to hold the grep line outputs and the multiple substitutions.

deox
  • 624
  • Thanks to all of you for helping! And I would certainly appreciate other methods of accomplishing this, or ways of improving the script. Thanks. – deox Mar 10 '13 at 20:06