Ubuntu – How to get a list of running applications by using the command line

command lineguiwindow

I want to list only running application like: Firefox, gedit, Nautilus, etc. using the command line.

Note: I don't want to list all running process, only applications which are running (say manually launched GUIs).

Best Answer

A combination of wmctrl and xprop offers many possibilities.

Example 1:

running_gui_apps() {

    # loop through all open windows (ids)
    for win_id in $( wmctrl -l | cut -d' ' -f1 ); do 

        # test if window is a normal window
        if  $( xprop -id $win_id _NET_WM_WINDOW_TYPE | grep -q _NET_WM_WINDOW_TYPE_NORMAL ) ; then 

            echo "$( xprop -id $win_id WM_CLASS | cut -d" " -f4- )"", window id: $win_id"

        fi

    done
}

The output could look in this case similar like this:

"Firefox", window id: 0x032000a9
"Gnome-terminal", window id: 0x03a0000c
"Thunar", window id: 0x03600004
"Geany", window id: 0x03c00003
"Thunar", window id: 0x0360223e
"Mousepad", window id: 0x02c00003
"Mousepad", window id: 0x02c00248
"Xfce4-terminal", window id: 0x03e00004

Example 2:

running_gui_apps() {
    applications=()

    # loop through all open windows (ids)
    for win_id in $( wmctrl -l | cut -d' ' -f1 ); do 

        # test if window is a normal window
        if  $( xprop -id $win_id _NET_WM_WINDOW_TYPE | grep -q _NET_WM_WINDOW_TYPE_NORMAL ) ; then 

            # filter application name and remove double-quote at beginning and end
            appname=$( xprop -id $win_id WM_CLASS | cut -d" " -f4 )
            appname=${appname#?}
            appname=${appname%?}

            # add to result list
            applications+=( "$appname" ) 

        fi

    done

    # sort result list and remove duplicates  
    readarray -t applications < <(printf '%s\0' "${applications[@]}" | sort -z | xargs -0n1 | uniq)

    printf -- '%s\n' "${applications[@]}" 
}

Output example:

Firefox
Geany
Gnome-terminal
Mousepad
Thunar
Xfce4-terminal

You can add the function to your ~/.bashrc or run it from an script file.