How to close all instances of a program instantly?
From command line:
killall file-roller
You can do alt+f4 but you need to do that for every archive manager that was opened.
Another approach would be to use the wmctrl
utility (Window Manager control). This can be used to tell the window manager to pretend you clicked the close button. This avoids the potentially heavy-handed nature of killall
; for example, some programs with an unsaved document will immediately exit when they are killed (even gently), while clicking the close button brings up a "Do you wish to save?" prompt.
The basic command is wmctrl -c WINDOWTITLE
, so in this case wmctrl -c "Archive Manager"
(assuming it has no open file: that changes the title). Unfortunately, it only closes one at a time, so we need to do more to close all of them.
wmctrl
returns success if it finds a match, so we can loop until it fails:
while wmctrl -c "Archive Manager"; do sleep 0.1; done
This always chooses the first window it finds, so we need to sleep for a bit to avoid continuously sending a stream of close commands to the first window that's already busy closing - that can cause an error which stops the loop.
This is simple and usually works, but sleeping a set amount of time and hoping a window closes before we try again is a messy and slow way to avoid the error. What we really want to do is to immediately send one close message to every matching window.
We can find all open windows with wmctrl -l
. This lists a numeric window id that we can use to identify each window individually, even if they all have the same title. Then we need to filter to only the matching windows (the grep
command), pull out just the window id (the cut
command) and call wmctrl -i -c
for each one. The -i
is needed to match the window id instead of the window title.
for w in $(wmctrl -l | grep "Archive Manager" | cut -d" " -f1); do
wmctrl -i -c $w
done
A bit complicated for just typing in whenever a cat steps on your keyboard, but hopefully a handy technique to keep in your scripting toolbox.