2

Something like this:

PID=3187: /home/morganc/test1.out

How do I use the find command so that it will list the PID number of my test1.out file as well as the directory of where it's located?

Thanks in advance.

  • are you sure you are not confusing find command with ps? – RomanPerekhrest Feb 13 '18 at 18:46
  • If the process that created or has the file open is still running, Gilles answer will work. If the process that created the file is no longer running, then no way to find out what happened in the past. – Mark Stewart Feb 13 '18 at 18:51
  • How did your file get a PID? – Jeff Schaller Feb 13 '18 at 18:53
1

You can try 2 solutions :

fuser /path/to/opened/file

or

lsof /path/to/opened/file
| improve this answer | |
1

If you are the owner of the process that created or has the file open, you can do

ls -o /proc/*/fd 2> /dev/null

which will list all files for PIDs for your user ID; if you don't have a lot of processes running, just manually look at the output. Sample: looking for process that has vmstat.out open:

mark (/home/mark)> ls -l /proc/*/fd 2> /dev/null
/proc/27669/fd:
total 0
lrwx------. 1 mark mark 64 Feb 13 19:51 0 -> /dev/pts/0
lrwx------. 1 mark mark 64 Feb 13 19:51 1 -> /dev/pts/0
lrwx------. 1 mark mark 64 Feb 13 19:53 10 -> /dev/pts/0
lr-x------. 1 mark mark 64 Feb 13 19:53 12 -> /home/mark
l-wx------. 1 mark mark 64 Feb 13 19:51 2 -> /dev/null
lrwx------. 1 mark mark 64 Feb 13 19:53 4 -> /home/mark/.sh_history

/proc/27737/fd:
total 0
lrwx------. 1 mark mark 64 Feb 13 19:53 0 -> /dev/pts/0
lrwx------. 1 mark mark 64 Feb 13 19:53 1 -> /dev/pts/0
lrwx------. 1 mark mark 64 Feb 13 19:53 2 -> /dev/pts/0
lr-x------. 1 mark mark 64 Feb 13 19:53 3 -> /home/oracle/log/vmstat.out
lr-x------. 1 mark mark 64 Feb 13 19:53 4 -> anon_inode:inotify

So PID 27737 has file vmstat.out open, and the full path is /home/oracle/log/vmstat.out

If you want to filter out most of the output, try this:

mark (/home/oracle/log)> ls -l /proc/*/fd 2> /dev/null | grep -E '(^/proc|vmstat.out)'
/proc/27669/fd:
/proc/27943/fd:
lr-x------. 1 mark mark 64 Feb 13 20:00 3 -> /home/oracle/log/vmstat.out
/proc/28047/fd:
/proc/28048/fd:
/proc/self/fd:
mark (/home/oracle/log)>

And then look for the PID right above the file of interest.

So to ignore the other processes, I piped to awk, saving the prior line, and printing the prior line when I found the current line started with an l:

ls -l /proc/*/fd 2> /dev/null | \
  grep -E '(^/proc|vmstat.out)'  | \
  awk 'BEGIN { prev = $0} /proc/ { prev = $0 } /^l/ { print prev  $NF }'

/proc/27943/fd:/home/oracle/log/vmstat.out
| improve this answer | |
1

Depending upon how I interpret your question (see comments), your solution would be either:

find $root_dir -name $file_name -exec pidof {} \;

or:

find $root_dir -name $file_name -exec fuser {} \;

Where you define variable $root_dir as the starting directory below which you want to perform your search, and $file_name as the name of the file for which you are looking.

The first alternative if for the case of searching for an executable file being run, and the second alternative is for the case of a data file being run by an executable.

To test this (for the first alternative), I opened a file using the less command, pressed C-z to stop it and put it in the background, and ran find /usr/bin -name less -exec pidof {} \;. To test it for the second alternative, I created a data file ~/test.txt, ran less ~/test.txt, then C-z, followed by find ~ -name test.txt -exec fuser {} \;.

| improve this answer | |
  • What if 20 people are running less at the time? I think the OP wants an output filename to look for. – Mark Stewart Feb 13 '18 at 19:29
  • 1
    I understood the question to be a search for the use of an executable, not a data file, so in your case, he would get twenty pids and a single path. I see, though, how you are interpreting the question differently, so I'll add a second solution in my answer to cover your case, thanks. – user1404316 Feb 13 '18 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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