4

How can specific windows be closed from the command line, without killing their controlling application?

Question based on an example to make it clearer:

I recently had an application running havoc and opening many firefox-windows. I needed to close them, without terminating firefox (since this would just restore the windows via firefox's session restoration). How can windows be "just closed" from the shell, like hitting the close-button?

2
  • 1
    Just wmctrl -c firefox doesn't work for you?
    – Pablo A
    Jan 2, 2019 at 20:11
  • @PabloBianchi: Yes, I did not know about this wmctrl. You can make this a full answer to my question. Jan 9, 2019 at 9:45

3 Answers 3

2

Try simply

wmctrl -c firefox

From wmctrl man page: Interact with a EWMH/NetWM compatible X Window Manager.

  • You can be more strict with -F option:

    -F Window name arguments () are to be treated as exact window titles that are case sensitive. Without this options window titles are considered to be case insensitive sub‐strings of the full window title.

  • To kill a specific window you can list them with wmctrl -l and then close it with wmctrl -ic ID.

Also answered here.

2

The combination of xlsw and xdotool does the trick.

xlsw lists windows by their title and type, xdotool does actions on them. In principle, xdotool can also search for windows' names, but it missed what were popup windows.

In bash:

xlsw | grep 'Firefox/Popup' | awk '{print $1}' | \
  while read _windowID; do xdotool windowclose "${_windowID}"; done

Explanation:

xlsw outputs a list of window-IDs together with additional information. Output looks like:

[...]
0x04800001  u--  Pale moon/pale moon  Pale Moon
0x04800092  uio  NA           Pale Moon
0x04800093  u-o  Pale moon/palemoon  Pale Moon
0x04800099  u-o  Pale moon/palemoon  Pale Moon
0x04828D21  u-o  Pale moon/Popup  Pale Moon
0x0489C5D6  u-o  Pale moon/Popup  Pale Moon
0x03600001  ui-  NA           NA
0x04604E77  --o  Claws-mail/claws-mail  claws-mail
0x04604EAC  --o  Claws-mail/claws-mail  claws-mail
0x04604F22  --o  Claws-mail/claws-mail  claws-mail
0x05C00001  uio  NA           NA
0x05E00001  u--  NA           NA
0x05200007  ui-  NA           NA
0x05E00002  u--  Wine/explorer.exe  
[...]

Then, with the grep-command, the popup-windows of firefox will be selected (in the example above they are all gone, already). awk prints the first entry, which is the window-ID, and then xdotool windowclose will close the windows (something like hitting the close button) without killing it's controlling application. (xdotool windowkill would kill the application.)

xdotool has also a build in search: xdotool search 'firefox' windowclose would also close all windows with 'firefox' in their title, but I did not get it to work to differentiate the 'Popup'.

2

I noticed on your profile that you are a user at Ask Ubuntu, so I will share the method that I use to select and close an open window from the shell on Ubuntu. The same method also works on many other Linux distrubtions.

  • wmctrl -mlpx lists the open windows with the window IDs and a description of each window. For Firefox the description is the title of the tab that has the focus and pop-up windows are listed as separate windows.

  • wmctrl -ic <window-id> closes a window that has id=window-id from the terminal.

If you don't have wmctrl installed, it can be installed with sudo apt install wmctrl in all currently supported versions of Ubuntu without requiring any other software from GitHub to be installed. There is also an .rpm of wmctrl in the Fedora package database.

1
  • On Arch Linux I also do not require to install from github. Just provided the upstream-URLs; I suspect the users are firm with their distributions's packaging system to search if it is available there. May 31, 2017 at 9:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .