Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have opened a PDF file with the document viewer from the GUI. Is there any way to get the path of this file in a terminal/script?

share|improve this question
    
file->properties will show you the current path of the opened file. –  Avinash Raj May 11 at 5:19
    
I want to have that in terminal and not using mouse –  lion May 11 at 5:59
1  
If you open file in terminal, you already now file location. Or we do not understand each other? –  c0rp May 11 at 6:11
    
no open file not from terminal and after that track PDF from terminal and so say the file location about track I should say some thing like process like ps command –  lion May 11 at 6:18
    
Your question don't reflect what you are saying in the comments, please clarify your question to reflect what you want exactly. –  i08in May 11 at 6:55

3 Answers 3

up vote 12 down vote accepted

Let say you have File.pdf

If you are using Document Viewer to read PDF, you are running /usr/bin/evince

Open your file.

Now find that process ID (PID) using this command:

$ ps -ef | grep "File.pdf"
c0rp     22291 22235 12 12:21 ?        00:00:02 /usr/bin/evince /home/c0rp/File.pdf

Or if you don't want to specify full name, use application name:

$ ps -ef | grep "evince"
c0rp     22291 22235 12 12:21 ?        00:00:02 /usr/bin/evince /home/c0rp/File.pdf

Here "22291" is the PID of Document Viewer using which you have opened File.pdf

To list all opened files by this PID we will use command lsof

$ lsof -F +p 22291
some other files opened
.
.
.
n/home/c0rp/File.pdf

Finally, to get only the file location, use this:

$ lsof -Fn +p 22291 | grep -i -E "*.pdf$" | sed s/^n//g
/home/c0rp/File.pdf

If you are interesting what is this magical n means, here is quote from man about -F option:

OUTPUT FOR OTHER PROGRAMS
       When the -F option is specified, lsof produces output that is  suitable
       for  processing by another program - e.g, an awk or Perl script, or a C
       program. 
...
...
       These  are  the  fields  that  lsof will produce.  The single character
       listed first is the field identifier.
...
...
            n    file name, comment, Internet address
...
...

so -Fn, is saying show me file name, comment, Internet address

share|improve this answer
1  
what a nice answers –  lion May 11 at 10:31

Another approach would be something like

$ for ip in $(pgrep -x evince); do lsof -F +p $ip  | grep -oP '^n\K.*\.pdf$'; done
/home/terdon/file1.pdf
/home/terdon/file2.pdf

Explanation

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. For example:

$ ps -ef | grep emacs
terdon    6647  6424 23 16:26 pts/14   00:00:02 emacs
terdon    6813  6424  0 16:26 pts/14   00:00:00 grep --color emacs
$ pgrep emacs
6647

The -x option returns only processes whose entire name matches the string passed. 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
4606 evince
4611 evinced
4613 evince
$ pgrep -lx evince
4606 evince
4613 evince

So, the for loop will run lsof on each of the PIDs returned by pgrep. These are then passed through grep. 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. In PCREs, \K means "discard everything matched before the \K ". In other words, since I am using-o, it will match lines beginning withnand ending with.pdfbut it will not print the matchedn`. The result is that only the file's name is printed.

share|improve this answer

You don't need to specify even the filename of the pdf opened through Document Viewer. This below command will display the paths of all the pdf files opened through Document Viewer.evince is the actual command to open Document Viewer via terminal.

ps -ef | grep evince | sed -n '/.*\.pdf/p' | sed 's/.*evince \(.*\)$/\1/g'

Example:

$ ps -ef | grep evince | sed -n '/.*\.pdf/p' | sed 's/.*evince \(.*\)$/\1/g'
/media/avinash/C68C57908C5779BF/pdf/PHP/PHP-Manual.pdf
/media/avinash/C68C57908C5779BF/pdf/python.pdf

But all the credit goes to @Corp.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.