Glossary

File Descriptor

Also known as: fd

A file descriptor (fd) is a small non-negative integer that a process uses to refer to an open file, pipe, socket, or other I/O object. The kernel keeps a per-process table mapping descriptors to open-file entries, which in turn point to inodes. When a program calls open("file.txt", O_RDONLY), it gets back an integer—say, 3—which it then passes to read, write, close, and so on.

Every process starts with three descriptors already open:

  • 0 — standard input (stdin)
  • 1 — standard output (stdout)
  • 2 — standard error (stderr)

Shell redirection manipulates these: > file redirects fd 1, 2> file redirects fd 2, 2>&1 duplicates fd 2 to fd 1, and so on. You can see a process's open descriptors under /proc/<pid>/fd/:

ls -l /proc/$$/fd
lsof -p $$                   # human-readable listing

File descriptors are inherited across fork and (by default) exec, which is how a shell passes the terminal's I/O to every program it starts. Closing or redirecting them in the right order is the heart of shell pipelines and daemon detachment.

Related terms: stdin, stdout, stderr, Pipe

Discussed in:

Also defined in: Textbook of Linux