3

Related, but not duplicates:

  1. /dev/fd inconsistency
  2. How does /dev/fd relate to /proc/self/fd/?
  3. Why does process substitution result in a file called /dev/fd/63 which is a pipe?

On Ubuntu 18.04 and 20.04, where Ubuntu has alias ll='ls -alF' defined in ~/.bashrc, I see the following outputs for the "floppy drive" /dev/fd devices:

For ls:

$ ls /dev/fd
0  1  2  3

For ll:

$ ll /dev/fd
lrwxrwxrwx 1 root root 13 Nov  5 11:46 /dev/fd -> /proc/self/fd/

I can see /dev/fd is a symbolic link to /proc/self/fd/, so I ll that and see:

$ ll /proc/self/fd/
total 0
dr-x------ 2 username username  0 Nov  8 19:01 ./
dr-xr-xr-x 9 username username  0 Nov  8 19:01 ../
lrwx------ 1 username username 64 Nov  8 19:01 0 -> /dev/pts/6
lrwx------ 1 username username 64 Nov  8 19:01 1 -> /dev/pts/6
lrwx------ 1 username username 64 Nov  8 19:01 2 -> /dev/pts/6
lrwx------ 1 username username 64 Nov  8 19:01 3 -> 'socket:[8239772]'
lr-x------ 1 username username 64 Nov  8 19:01 4 -> /proc/29512/fd/
  1. What does all this stuff mean?
  2. How do floppy disks work here?
  3. What is /dev/pts/6, and why do 0, 1, and 2 all point to it?
  4. What is 'socket:[8239772]'?
  5. What is /proc/29512/fd/?
  6. What is the historical use of and description for /dev/fd?

Related:

  1. my previous question which inspired me to ask this question: '-sh: syntax error: unexpected "("' when attempting process substitution on an embedded Linux device with `bash`
4
  • This just seems to be asking for restating what's already in those questions. "Do the two fd's mean both floppy disk, both file descriptor, or one for each?" ... "They mean file descriptor in each case. " <- clearly floppy disks have nothing to do here, so it looks like you haven't even bothered to read the questions you say aren't duplicates.
    – muru
    Commented Nov 9, 2021 at 2:17
  • @muru, multiple sources online actually state fd in /dev/fd means "floppy disk". Ex: here and here, so it is confusing to say the least. This still isn't clear. I wonder if fd originally meant "floppy disk" and then gradually came to mean "file descriptor" when floppy disks went out of use. Also, my question goes way beyond that, and beyond what the other answers contain. Commented Nov 9, 2021 at 2:26
  • 2
    It's /dev/fd0 that's the floppy, not /dev/fd. fd != fd0. I'm pretty sure Unix on the PDP-11 predates floppy disks.
    – muru
    Commented Nov 9, 2021 at 2:37
  • 1
    @GabrielStaples, if you look at that second link in your comment, you see the line fd 0 0 0 0% /dev/fd given as output of df -k. (A more complete output here.) That looks similar to the df -k output on Linux in that the first field is the device and the last is the mount point. With the device called just fd, and not /dev/... and sizes shown as zeroes, I'd be willing to bet it's a virtual filesystem that's the Solaris equivalent of Linux's /proc/*/fd, and nothing to do with floppies.
    – ilkkachu
    Commented Nov 9, 2021 at 17:57

1 Answer 1

10

What does all this stuff mean?

/proc is a filesystem through which the kernel reports various information to processes. It's mostly for information about processes, hence the name “proc[esses]”. For each running process, there's a subdirectory /proc/<PID> where <PID> is the process ID.

/proc/self is a “magic” symbolic link that always points to the process that's accessing /proc.

/proc/self/fd reports the files opened by a process. Each entry is a “magic” symbolic link whose name is the file descriptor and whose target is the open file. It's magic in the sense that the link actually points to the file itself, even if the file name obtained by calling readlink is not a valid file name, which happens, for example, with files that don't have a name (such as anonymous pipes and sockets), and with deleted files.

lrwx------ 1 username username 64 Nov  8 19:01 0 -> /dev/pts/6
lrwx------ 1 username username 64 Nov  8 19:01 1 -> /dev/pts/6
lrwx------ 1 username username 64 Nov  8 19:01 2 -> /dev/pts/6
lrwx------ 1 username username 64 Nov  8 19:01 3 -> 'socket:[8239772]'
lr-x------ 1 username username 64 Nov  8 19:01 4 -> /proc/29512/fd/

How do floppy disks work here?

No floppy disks are involved. The abbreviation “fd” stands for file descriptor.

What is /dev/pts/6, and why do 0, 1, and 2 all point to it?

File descriptors 0, 1 and 2 are the three standard streams) that all programs expect to find: standard input (stdin), standard output (stdout) and standard error (stderr). These streams are defined by their number: stdin is file descriptor 0 by definition, and its conventional role is to receive user input or input data, and likewise for stdout (1, user output or output data) and stderr (2, error messages).

/dev/pts/6 is a terminal. It's the file that processes use to read input from, and write output to, that particular terminal. When you run a program “normally” in a terminal, stdin, stdout and stderr are all connected to the terminal.

What is 'socket:[8239772]'?

It's a socket. File descriptor 3 has no standard role, so it's specific to some software you're using, maybe your terminal emulator. You can see what's on the other end of the socket if you're curious.

What is /proc/29512/fd/?

When you run ls /proc/self/fd/, the ls program opens /proc/self/fd to read its contents. Since file descriptors 0 to 3 are already open when ls starts, and open uses the first available file descriptor, /proc/self/fd ends up being open on file descriptor 4. It appears with the PID because the kernel doesn't keep track of accesses through /proc/self internally: it remembers them as a particular process's /proc directory, which it then prints out using the correct PID. (With PID namespaces, that PID, and hence the path, can be different depending on which process is looking.)

What is the historical use of and description for /dev/fd?

It's useful when programs need a file name and you want to refer to a file that's already open. It's a generalization of /dev/stdin, /dev/stdout and /dev/stderr which are equivalent to /dev/fd/0, /dev/fd/1 and /dev/fd/2 respectively (and existed before the more general /dev/fd appeared). It's available on many unix variants.

2
  • Thank you for this thorough answer which covered all 6 of my subquestions! I'm bookmarking this as a rather canonical answer to understand and remember what /dev/fd file descriptors mean and are, among many other things I learned here, such as /proc/PID, and /dev/pts/6 terminal, etc. Commented Nov 9, 2021 at 17:12
  • It's impressive that even you get downvoted (not just my question, but your answer too) despite it being such a high-quality and useful answer which combines and contains information not contained in the other sources already present on this site. Commented Nov 9, 2021 at 21:29

You must log in to answer this question.

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