Shell – Is there something like an “xdg-close” – opposite of xdg-open

shell-scriptx11xdg-open

Sometimes I xdg-open somefile.ext from a terminal session, but then I want to be able to close the application or viewer that opened, later on. I was hoping there might be something like an xdg-close, which examines the existing windows to see which of them reports having somefile.ext open, and close/kill that application.

Does something like this exist under a different name?

Best Answer

Assuming that the file you opened isn't deleted, the following will forcibly close all applications that have it open:

fuser -k -TERM FILE

Replace FILE with the name of the file in question.

Note that this is potentially very dangerous if you are not careful. If you accidentally pass in your home directory for example, it will terminate all current login sessions you have on the system (both graphical and textual), as well as killing most of your background processes.

Assuming you know which application opened the file, there are other somewhat safer desktop environment specific methods to do this, but I don't know enough about them to give you good advice here.

Now, as to why there is no xdg-close command:

xdg-open exists so that tools like file managers or web-browsers can make sure the user's preferred application gets invoked when they try to open a given file. In other words, it originated so that you don't have to go through every application on your system when you want to change what is used to open a file, but can instead set defaults in one place, and the applications don't have to care what desktop environment you're using when they want to open something in another application. It can be called from the command-line by hand, but that's not really what it's designed for.

Automating closing applications that opened a specific file is not exactly something that's all that user friendly. Your web browser has no business closing the PDF viewer you opened to view the PDF you downloaded, so why does it need a tool that lets it do so? Additionally though, your desktop environment doesn't track what applications have what files open (the OS tracks it as what processes (which do not map 1:1 to applications) have what files open), so there's not really any easy way to implement this either.

For what it's worth, the only reason xdg-open exists as a command at all is because it originated before DBus became part of the FreeDesktop.org specification, otherwise it would almost certainly be a DBus API call provided by yet another unnecessary background process.

Related Question