Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Concepts

File descriptors

What a Linux file descriptor is, how the shell opens, duplicates and closes them with exec and redirections like 3>file and 2>&1, and how to inspect them.

A file descriptor (fd) is a small non-negative integer that a process uses to refer to something it has opened: a regular file, a directory, a pipe, a terminal, a socket or a device. The kernel keeps a table per process that maps each number to an open file; system calls such as read, write and close take the number, not a name.

Three descriptors exist before your program runs a single line: 0 is standard input, 1 is standard output and 2 is standard error (see stdin, stdout and stderr). Everything you open afterwards gets the lowest free number, usually 3. The shell exposes this table directly through redirection syntax, which is why 2>&1 and exec 3<file work the way they do.

Terminal
exec 3< input.txt      # open fd 3 for reading
read -r line <&3       # read from it
exec 3<&-              # close it
ls -l /proc/$$/fd      # list this shell's descriptors

Try it

Bash Open, use and close a descriptor

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Duplicating descriptors and looking at /proc

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

How descriptors work

The table and the lowest-free rule

Each process has its own table. open() returns the lowest unused number, which is why a program that closes stdin (0) and then opens a file gets that file as its new stdin. Child processes inherit copies of the table at fork, pointing at the same open files, so a parent and child share the read position of an inherited descriptor.

Shell syntax

Form Meaning
n< file Open file for reading as fd n (default n is 0).
n> file Open for writing, truncating, as fd n (default 1).
n>> file Open for appending.
n<> file Open for reading and writing.
n>&m Make fd n a duplicate of fd m (output). n<&m is the input form.
n>&- / n<&- Close fd n.
exec n< file Open the descriptor permanently in the current shell instead of for one command.
> file Bash picks a free number 10 or higher and stores it in $var.
/dev/fd/N, /proc/self/fd/N A path that names descriptor N, usable where a file name is required.

Errors and edge cases: reading from or writing to a closed descriptor prints "Bad file descriptor" and fails. Order matters in redirections: cmd > f 2>&1 sends both to the file, while cmd 2>&1 > f sends only stdout to the file and stderr to the old stdout.

Usage notes

  • Limits. Each process may hold only a limited number of descriptors (ulimit -n, often 1024 by default); leaking them in a long-running program eventually fails with "Too many open files". See ulimit.
  • Close what you open. Writes through a descriptor are only guaranteed complete once the descriptor is closed or the process exits; close before reading the file back.
  • Descriptors above 9 are safest for your own use in Bash, as 3-9 may be used internally by some commands. exec {fd}<file avoids clashes.
  • See who has a file open with lsof, or look at ls -l /proc/PID/fd for one process: lsof.
  • A deleted file stays alive while any descriptor refers to it; the space is freed only when the last one closes. That is why a full disk may not recover after rm until the program restarts.
  • Inheritance. Descriptors leak into child programs unless marked close-on-exec; in scripts close extras with 3>&- on the command that does not need them.
  • Pipes, sockets and devices are descriptors too, so the same redirections work with /dev/tcp and named pipes: /dev/tcp, mkfifo.