I have multiple instances of Firefox running in Ubuntu 14.04. How can I close the most recently opened window/instance from the Terminal?

I tried using the cfct alias defined in an answer to a related question, but it didn’t work.

  • @chaos I assume OP means windows, not tabs May 1, 2015 at 8:57
  • @JacobVlijm that answer also closes windows and seems for me the only solution. Since firefox runs only one process, regarless how many windows are open, you need that solution in the dublicate. killall firefox would SIGTERM the whole process, not only a window.
    – chaos
    May 1, 2015 at 9:01
  • @chaos I don't agree, very well possible, I am not at home, see if I am quick enough to produce an answer in my break :) May 1, 2015 at 9:03
  • 1
    @chaos Duplicates are for when the questions are the same, not the answers. Some overlap is unavoidable.
    – Seth
    May 7, 2015 at 21:19
  • What is cfct? Searches mainly return this very question.
    – TRiG
    May 9, 2015 at 17:50

2 Answers 2


To make a command that finds the id of the last window, created by Firefox (and to close it), you will need wmctrl to be installed:

sudo apt-get install wmctrl

The command

Then use the command:

wmctrl -ic "$(wmctrl -l | grep 'Mozilla Firefox' | tail -1 | awk '{ print $1 }')"


wmctrl -l

lists all windows, but an important property of the command is that it lists the windows in the order they were created.


wmctrl -l | grep 'Mozilla Firefox' | tail -1 | awk '{ print $1 }'


  • list all windows:

    wmctrl -l
  • find the ones (the lines) with 'Mozilla Firefox' in their name:

    grep 'Mozilla Firefox'
  • find the last one (which is also the last created one):

    tail -1
  • extract the first string in the line (which is the window -id):

    awk '{ print $1 }'

The command:

wmctrl -ic

will then kill the most recent Firefox window by its id (gracefuly).

Or even more reliable:

While the command above works well in practically all cases, there is a small chance of name clashes, if e.g. another window exists with "Mozilla Firefox" in its name, but not a window from Firefox (unlikely, but still).

What should work "waterproof" is therefore to identify the windows in the window list (using wmctrl -lp) by the pid of firefox, instead of the string in the window name:

wmctrl -ic "$(wmctrl -lp | grep "$(pgrep firefox)" | tail -1 | awk '{ print $1 }')"

As you can seen, in this command,

wmctrl -l | grep 'Mozilla Firefox'

producing the lines containing 'Mozilla Firefox', is replaced by:

wmctrl -lp | grep "$(pgrep firefox)"

producing the lines containing the pid of firefox (as the output of pgrep firefox)


To close a window (also with many opened tabs) use that command:

wmctrl -a firefox; xdotool key Ctrl+Shift+w

Notice, that wmctrl and xdotool must be installed:

$ sudo apt-get install wmctrl xdotool

See also that answer: Close current tab firefox using terminal.

My answer is a bit modified, because Ctrl+Shift+w closes a firefox window.

For all shortcuts, see Firefox Keyboard shortcuts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.