5

I know the file descriptor of a file opened by a process, but I don't know the process ID. How can I print file names from the Linux command prompt if I know the file descriptor of a file opened by a process?

2
  • @tohuwawohu It is the list of files using process ids. Here in this case I don't have process id only file decriptor
    – techfun
    Feb 12, 2014 at 19:53
  • ah, ok, sorry, didn't get that point. So you're right, it isn't a dup.
    – tohuwawohu
    Feb 12, 2014 at 19:55

2 Answers 2

5

If you do not know the process ID, you will have to check all processes that have the same fd# open, since file descriptors are not globally unique. The smaller the fd#, the more processes will have it open (e.g. on my system, even if the fd# is around 30, I'd still need to guess between 15 processes, and if I was looking for fd# around 10, then the list would have ~170 processes).

The proc filesystem shows the file descriptors as symlinks under /proc/<pid>/fd.

# ls -l /proc/1/fd
lrwx------ 1 root root 64 Feb 12 22:10 /proc/1/fd/0 -> /dev/null
lrwx------ 1 root root 64 Feb 12 22:10 /proc/1/fd/1 -> /dev/null
lrwx------ 1 root root 64 Feb 12 22:10 /proc/1/fd/2 -> /dev/null
l-wx------ 1 root root 64 Feb 12 22:10 /proc/1/fd/3 -> /dev/kmsg
lrwx------ 1 root root 64 Feb 12 22:10 /proc/1/fd/4 -> anon_inode:[eventpoll]
lrwx------ 1 root root 64 Feb 12 22:10 /proc/1/fd/5 -> anon_inode:[signalfd]
lr-x------ 1 root root 64 Feb 12 22:10 /proc/1/fd/6 -> /sys/fs/cgroup/systemd/
...etc...

For example, to look for fd #5 in all processes:

# ls -l /proc/*/fd/5
lrwx------ 1 root    root    64 Feb 12 22:10 /proc/1/fd/5 -> anon_inode:[signalfd]
lrwx------ 1 root    root    64 Feb 12 22:15 /proc/129/fd/5 -> socket:[6980]
lrwx------ 1 root    root    64 Feb 12 22:15 /proc/168/fd/5 -> socket:[7847]
lrwx------ 1 root    root    64 Feb 12 22:15 /proc/341/fd/5 -> anon_inode:[eventfd]
lr-x------ 1 root    root    64 Feb 12 22:15 /proc/342/fd/5 -> anon_inode:inotify
...etc...

The exact interface to resolve symlink targets is readlink():

# readlink /proc/427529/fd/7
/home/grawity/lib/dotfiles/vim/backup/%home%grawity%.bashrc.swp
1
  • Very good answer! Now even i understand what's going on :-)
    – tohuwawohu
    Feb 12, 2014 at 20:19
1

From the lsof manpage:

To find the process that has /u/abe/foo open, use:

lsof /u/abe/foo

See also this tutorial on lsof ans these hints on lsof

5
  • Thanks for response. But Actually.I don't know in this case "/u/able/foo file". Only file descriptor is available
    – techfun
    Feb 12, 2014 at 20:06
  • Could you please give an example?
    – tohuwawohu
    Feb 12, 2014 at 20:06
  • I know file descriptor(integer number) of opened by a random process which is getting started on server. It stays open after process call. I need to know the file path.
    – techfun
    Feb 12, 2014 at 20:08
  • 1
    Sorry, i'm completely confused. This answer tells you how to get all the file names including the path if you know the process. So either you know the file to check, then my answer applies, or you know the process, then the linked answer applies. I simply can't see what's missing :-(
    – tohuwawohu
    Feb 12, 2014 at 20:13
  • I think there is some lsof incantation that would list a specific fd# across all processes, but I have no idea what it looks like. Feb 12, 2014 at 20:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .