2
cd /dev/fd/; ls

Gives me output that I expect:

0  1  2  255

But:

ls /dev/fd/

Gives different output that I don't expect:

0  1  2  3

Why? What happens here? What happened to file descriptor 255? What is file descriptor 3?

3
cd /dev/fd/; ls

shows the shell’s file descriptors: cd changes to the shell’s file-descriptor directory, and ls shows its contents.

ls /dev/fd/

shows ls’s file descriptors. File descriptor 3 is the directory being listed by ls:

ls -l /dev/fd/

will show that it points to /proc/.../fd, where ... is the ls process identifier.

It might be easier to understand this if you look at /dev/fd itself:

ls -ld /dev/fd

On Linux, it points to /proc/self/fd.

  • That's interesting that cd [path]; ls and ls [path] are in fact not equivalent. Thanks to your answer I just invented a game based on this: you can try to predict ls's process id by trying: ls /proc/self/task/[guessed id] :) . – Łukasz Zaroda Dec 12 '18 at 21:24

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.