63

I am running in an interactive bash session. I have created some file descriptors, using exec, and I would like to list what is the current status of my bash session.

Is there a way to list the currently open file descriptors?

5
  • 1
    check this link
    – Kamaraj
    Dec 28, 2016 at 4:54
  • @Kamaraj thanks. So bash has no built-in to do that? I need to use linux-specific features?
    – blueFast
    Dec 28, 2016 at 4:57
  • bash is not part of the kernel, so it can not know what other processes are doing, even those that it started. Dec 28, 2016 at 4:58
  • @JuliePelletier: but child processes do not change the file descriptors of the parent process, do they?
    – blueFast
    Dec 28, 2016 at 5:09
  • hi, what do you mean by "I have created some file descriptors, using exec"?
    – Chan Kim
    Feb 19, 2021 at 1:53

5 Answers 5

72

Yes, this will list all open file descriptors:

$ ls -l /proc/$$/fd
total 0
lrwx------ 1 isaac isaac 64 Dec 28 00:56 0 -> /dev/pts/6
lrwx------ 1 isaac isaac 64 Dec 28 00:56 1 -> /dev/pts/6
lrwx------ 1 isaac isaac 64 Dec 28 00:56 2 -> /dev/pts/6
lrwx------ 1 isaac isaac 64 Dec 28 00:56 255 -> /dev/pts/6
l-wx------ 1 isaac isaac 64 Dec 28 00:56 4 -> /home/isaac/testfile.txt

Of course, as usual: 0 is stdin, 1 is stdout and 2 is stderr.
The 4th is an open file (to write) in this case.

2
  • 2
    what is /proc/$$ ? specifically how does "$$" work?
    – JZ.
    Mar 2, 2020 at 17:14
  • 3
    @JZ.$$ will give the process ID of the currently running shell. Try echo $$ and ps and compare their outputs. Similar question here. May 23, 2020 at 18:40
11
lsof -a -p $$

Network fd only:

lsof -i -a -p $$
9

Assuming you want to list the file descriptors that are attached to any terminal, you can use lsof/fuser or similar like:

$ lsof -p $$ 2>/dev/null | awk '$NF ~ /\/pts\//'
bash    32406 foobar    0u   CHR 136,31      0t0      34 /dev/pts/31
bash    32406 foobar    1u   CHR 136,31      0t0      34 /dev/pts/31
bash    32406 foobar    2u   CHR 136,31      0t0      34 /dev/pts/31
bash    32406 foobar    3u   CHR 136,31      0t0      34 /dev/pts/31
bash    32406 foobar  255u   CHR 136,31      0t0      34 /dev/pts/31

These tools basically parse /proc, so you can just access /proc/$$/fd/ too e.g.:

ls /proc/$$/fd/*
3
  • 4
    The selection of \/pts\/ rejects open files like exec 4>testfile.txt
    – user232326
    Dec 28, 2016 at 5:03
  • @sorontar Yep, like i said this one is only for listing the ones attached to any terminal.
    – heemayl
    Dec 28, 2016 at 5:06
  • ls /proc/$$/fd/* doesn't work for me. I get ls: /proc/48855/fd: No such file or directory :( May 19, 2021 at 22:48
5

Use the lsof utility to print all file descriptors for the current shell process (process identified by -p $$) and (-a) where the file descriptor is numeric (-d 0-256):

$ lsof -p $$ -a -d 0-256
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
bash    16883  ant    0u   CHR 136,15      0t0   18 /dev/pts/15
bash    16883  ant    1u   CHR 136,15      0t0   18 /dev/pts/15
bash    16883  ant    2u   CHR 136,15      0t0   18 /dev/pts/15
bash    16883  ant  255u   CHR 136,15      0t0   18 /dev/pts/15

Pipe into Awk to print only the file descriptor and its corresponding filename:

$ lsof -p $$ -a -d 0-256  | awk '{ printf("%4s:\t%s\n", $4, $NF) }'
  FD:   NAME
  0u:   /dev/pts/15
  1u:   /dev/pts/15
  2u:   /dev/pts/15
255u:   /dev/pts/15

Note: when lsof prints the file descriptors, it appends the following code to indicate the file access mode:

  • r – read access
  • w – write access
  • u – read and write access
0

If you happen to want a graphical solution, gnome-system-monitor allows you to see the opened file descriptors of a process. Right click on any process opens a contextual menu, then you can click Open Files. Or you can just select the process and press CTRL+O.

Bonus: There is also an option in the sandwich menu to search opened files by filename

Screenshot of System Monitor

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.