What are these files in my /dev directory?

Solution 1:

These files are actually called stdin, stdout and stderr. The @ character is added by ls to tell you that they are symbolic links. ls -l would reveal that the targets of these symbolic links are /proc/self/fd/0, /proc/self/fd/1 and /proc/self/fd/2.

/proc is a virtual filesystem provided by the kernel that shows information about the operating system. Among other things, there are directories like /proc/1234 that contain information about the process with ID 1234. /proc/self is a symbolic link to the directory for whatever process is accessing it (the kernel returns a different target to different processes).

/proc/self/fd (which is also symlinked at /dev/fd) contains one entry per file that the process has open. Each entry corresponds to a file descriptor and is a symbolic link to the file (if possible — for pipes, sockets and deleted files, the symbolic link gives information that's useful for debugging but does not point to a file that you could reopen by name). These file descriptors are the same that you manipulate with C functions such as open, close, dup, read, write, etc. They are not used by the C functions, they're other ways to interact with the same objects.

The reason /dev/stdin and friends exist is that sometimes a program requires a file name, but you want to tell it to use a file that's already open (a pipe, for instance). So you can pass /dev/stdin to tell the program to read its standard input.

Further reading:

  • file descriptor vs. file name
  • file descriptors and /dev/fd
  • How does /dev/fd relate to /proc/self/fd/?
  • Understanding /dev and its subdirs and files
  • How to include program which only works with in/out files into a pipeline?