How to close specific X11-windows from the shell?

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'.


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.


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.