Shell – Linux Shell fundamentals: How to check the file descriptors status, (where are redirected to) right now without lsof

cygwin;io-redirectionlinuxshell

I am learning about file descriptors for the case of Linux, and I was wondering if there is any command showing a complete list that allows me to know where is each one redirected to in this moment.
Maybe some info like:

0  -->  <stdin
1  -->  >stdout
2  -->  >stderr
3  -->  >MyFileForWriting
4  -->  <MyFileForReading

I have found this thread that requires lsof, but it could be useful to have another method not depending on that command, if it exists.
I need this info concerning to Linux (I will try it too for CygWin, that does not include the 'lsof' tool until now, but behaves mostly like a POSIX compliant operating system).

Best Answer

The /proc file system will list exactly this information:

$ ls -l /proc/self/fd
total 0
lrwx------ 1 michas users 1 Apr  6 04:44 0 -> /dev/pts/0
lrwx------ 1 michas users 1 Apr  6 04:44 1 -> /dev/pts/0
lrwx------ 1 michas users 1 Apr  6 04:44 2 -> /dev/pts/0
lr-x------ 1 michas users 1 Apr  6 04:44 3 -> /proc/6934/fd
$ ls -l /proc/self/fd 2>/dev/null <<<foo |cat
total 0
lr-x------ 1 michas users 1 Apr  6 04:45 0 -> /tmp/sh-thd-361068043 (deleted)
l-wx------ 1 michas users 1 Apr  6 04:45 1 -> pipe:[136729]
l-wx------ 1 michas users 1 Apr  6 04:45 2 -> /dev/null
lr-x------ 1 michas users 1 Apr  6 04:45 3 -> /proc/6952/fd

If you are interested in some other process just replaces "self" with the corresponding PID.

Related Question