Evince pdf reader command line loop
![evince pdf reader command line loop evince pdf reader command line loop](http://elinuxbook.com/wp-content/uploads/2017/09/install-evince-linux-pdf-viewer-in-ubuntu-16-04-a-document-viewer-for-linux.png)
The -o option means "print only the matched portion of the line" and the -P activates Perl Compatible Regular Expressions which lets us use \K. So, the for loop will run lsof on each of the PIDs returned by pgrep. This is needed because evince also starts a daemon ( evinced) and that will also be matched without the -x (the -l is to print the name as well as the PID): $ pgrep -l evince The -x option returns only processes whose entire name matches the string passed. In general, whenever you want to search fro a process, pgrep is better than ps -ef | grep process since the latter will also match the grep process itself.
![evince pdf reader command line loop evince pdf reader command line loop](https://image.slidesharecdn.com/pyladiesfallinlovewithpython-150222142044-conversion-gate01/85/pyladies-talk-learn-to-love-the-command-line-17-320.jpg)
So -Fn, is saying show me file name, comment, Internet addressĪnother approach would be something like $ for ip in $(pgrep -x evince) do lsof -F +p $ip | grep -oP '^n\K.*\.pdf$' done These are the fields that lsof will produce.
![evince pdf reader command line loop evince pdf reader command line loop](https://www.tecmint.com/wp-content/uploads/2016/03/qpdfview-for-Linux.png)
When the -F option is specified, lsof produces output that is suitableįor processing by another program - e.g, an awk or Perl script, or a C If you are interested in what n in lsof -Fn is for, here is quote from man lsof about the -F option: OUTPUT FOR OTHER PROGRAMS This one-liner was inspired from the answer of terdon which is also very interesting in the way it solves the same problem. Next we'll grep only for pdfs and discard the irrelevant n at start of line: $ lsof -Fn +p 22291 | grep -i '^n.*\.pdf$' | sed s/^n//gįinally to combine everything in one bash line: for ip in $(pgrep -x evince) do lsof -F +p $ip|grep -i '^n.*\.pdf$'|sed s/^n//g done To list all files opened by this PID we will use the lsof command (note that we'll need to repeat this for every PID in case we have more than one instance of evince running) $ lsof -F +p 22291 So first we need to find the process ID (PID) of evince: $ pgrep -x evince TLDR: for ip in $(pgrep -x evince) do lsof -F +p $ip|grep -i '^n.*\.pdf$'|sed s/^n//g doneĭocument Viewer is the friendly name for the program /usr/bin/evince.