retrieving names of all open pdf files (in evince or otherwise)

Under the assumption that the PDFs you are viewing have the extension .pdf, the following could work to get you a list of open PDFs:

$ lsof | grep ".pdf$"

If you only ever use Evince, see Gilles' similar answer. On my machine (with a few pdfs open) displayed output as follows

evince    6267     myuser   14u      REG    252,0   363755    7077955 /tmp/SM.pdf

To just get the filenames, we can use awk:

$ lsof | grep ".pdf$" | awk '{print $9}'

or even better,

$ lsof | awk '/.pdf$/ {print $9}'

We can save these results in a file:

$ lsof | awk '/.pdf$/ {print $9}' > openpdfs

And later, to restore them:

$ xargs -a openpdfs evince

To make this happen automatically, you can use whatever mechanisms your desktop environment provides to run the "save" command on exit and the "open" command on login. A bit more robustness can be added by ensuring that the pdf's returned by lsof are being opened by your user. One advantage of this method is that it should work for any pdf viewer that takes command-line arguments. One disadvantage is that it depends on file names; however, with a bit of poking, the requirement of knowing the filename extension could probably also be removed.


lsof lists the files open by a process. Evince does keep the files open. If you're the only user using evince, under Linux, here's how to see what files it's using:

lsof -p $(pgrep -d, -x evince)

To automate this, you'll want to keep only the open regular files of the evince process. Here's a script that outputs the file names.

lsof -n -d0-9999 -Fpctn |  # parseable output, pid/command/type/name, only open files
sed 's/^p/\np/' |  # split each process into a paragraph
awk -vRS= '/\ncevince\n/ {print}' |  # retain only paragraph(s) with command="evince"
sed 's/^t/\nt/' |  # split each file into a paragraph
awk -vRS= '/^tREG\n/ {print substr($0, 7);}'  # retain only regular files