$_ CLI In Depth
Learning progress 0%
lesson syscalls namespaces cgroups

Open Files & the FD Table

5 min read Module 17: Syscalls, Namespaces & cgroups Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:25

The descriptor table

Each process has a file descriptor table, an array indexed by small integers. When a program opens a file, the kernel stores a reference to the open file in the lowest free slot and returns that slot number. The number is called a file descriptor. Programs use the number in later calls such as read, write, and close, and never see the internal file structure.

By convention, descriptor 0 is standard input, 1 is standard output, and 2 is standard error. A shell sets these up before it starts a command. In an interactive terminal, all three point at the same pseudo-terminal, which is why error messages and normal output both appear on the screen.

Looking at the table

The directory /proc/PID/fd contains one link for each open descriptor. The name of the link is the number, and the target is the file it refers to. For a terminal the target is a device such as /dev/pts/0. For a file it is a path, and for a pipe the target is a name such as pipe:[12345].

BASH1 lines
ls -l /proc/self/fd

Each line shows a descriptor and where it points. In the lab, descriptors 0, 1, and 2 point at the pseudo-terminal, unless the command was started with a redirection. The number of the listing command itself is also open briefly, because the directory is open while ls reads it.

Redirection changes the table

A redirection is a request to change a slot before the command runs. The sequence cmd > out.txt opens out.txt, places it in slot 1, and then starts cmd. The program only sees slot 1 and writes to it without knowing that the target changed. Redirections are processed left to right, and each one copies or opens a descriptor for the next step.

The order matters. Consider ls /nope > out.txt 2>&1. First slot 1 is set to out.txt. Then slot 2 is made a copy of slot 1, so both standard output and error go to the file. Reversing the two redirections sends error to the terminal and only standard output to the file, because slot 2 was copied before slot 1 changed.

BASH1 lines
ls /nope > stdout.txt 2> stderr.txt; wc -c stdout.txt stderr.txt

This separates the two streams completely. The standard output file stays empty because ls printed nothing useful, and the error file holds the message. Scripts that separate output and errors depend on this behavior.

exec and persistent descriptors

The exec builtin, used without a command, changes descriptors of the current shell. The line exec 3> notes.txt opens the file on descriptor 3 for the rest of the script or session. Any command run after it inherits the descriptor, unless it was marked close-on-exec. Writing echo hi >&3 then sends text to the file. The line exec 3>&- closes it.

This technique lets a script open a log file once and write to it from many commands, without repeating the path in every redirection.

Sharing after fork

When bash forks a child, the child gets a copy of the descriptor table. The entries refer to the same open file descriptions, so the two processes share the same file offset. This is why a pipeline such as (echo a; echo b) | cat works: both commands write to one pipe, and the reader sees the bytes in order. It is also why a child that reads from a file moves the offset for its parent.

Warning

Descriptors you open with exec stay open for every command in that shell. Close them with exec 3>&- when you are done, or a later command may write to the wrong file.

Limits and practice

The table has a per-process limit, which you can read with ulimit -n. A program that opens files without closing them eventually fails with a too-many-files error. In the lab, that limit is high enough for these exercises, but the habit of closing what you open is the same everywhere.

Pipes are descriptors too

A pipe is a pair of descriptors created by the pipe system call, one for reading and one for writing. The shell creates the pipe, forks each stage of the pipeline, and rewires slot 0 of the reader and slot 1 of the writer to the pipe ends before each program runs. The programs in the pipeline do not know a pipe exists. A listing of the descriptors inside a pipeline shows a target such as pipe followed by a number for the slots the shell connected.

This also explains why a pipeline stays open until every stage ends. Each stage holds its own descriptors, and closing the write end is what tells the reader that no more input is coming. When a stage exits, its descriptors close, and the next stage sees end of file. A program that keeps a copy of a write end open never sees end of file, and that is a common reason a pipeline hangs.

Descriptor leaks

A program that opens files in a loop and forgets to close them is leaking descriptors. The symptom is an error about too many open files, and the diagnosis is the same listing as before. Count the lines in the fd directory and look for a target that repeats, because repeated links to the same file are the signature of a leak. The shell limit is the number that ulimit reports for open files, and you can read the current value with ulimit with the n option.

What cat /etc/hostname asks the kernel

Visualizer

Every program is a list of system calls. strace prints each call with its arguments and return value, so you can read what a command really does, in order.

1 / 6
strace of cat /etc/hostname (trimmed)
execve("/usr/bin/cat", ["cat", "/etc/hostname"], 0x7ffd5c2b8f30 /* 9 vars */) = 0

This trace is trimmed to the calls that matter. execve() replaces the forked child’s program with cat. The quoted strings are argv. The trailing = 0 is execve() reporting success; strace prints it once the new program is loaded.

strace -e trace=execve,openat,read,write,close,exit_group cat /etc/hostname 2>&1 | tail -n 9
Terminal lab · 3 steps · real Linux in your browser

Inspect open file descriptors

List the descriptor table, open an extra descriptor, and see how redirection order changes where errors go.

01
List the descriptors of a process
Each entry in /proc/PID/fd is a descriptor, and the name of the link is its number. Save the list of your own descriptors: ``bash ls -l /proc/self/fd > fds.txt `` Open fds.txt. Descriptors 0, 1, and 2 are standard input, output, and error, and each one points at a file or device.
Show hint for step 1
The number before the arrow is the descriptor. Look for the lines that end with 1 -> and 2 -> followed by a path.
02
Open descriptor 3 by hand
Open a file on descriptor 3 with exec, list the descriptors while it is open, then close it: ``bash exec 3> three.txt ls -l /proc/self/fd > fds-with-3.txt exec 3>&- `` Open fds-with-3.txt and find the line for descriptor 3. It should point at three.txt.
Show hint for step 2
exec with only a redirection changes the descriptors of the current shell. exec 3>&- closes descriptor 3.
03
Send stdout and stderr to separate files
Run ls on a missing path, sending standard output to stdout.txt and standard error to stderr.txt: ``bash ls /nope > stdout.txt 2> stderr.txt `` The error message goes to stderr.txt and stdout.txt stays empty. Check both files.
Show hint for step 3
Each redirection is applied to a descriptor number: > is 1 and 2> is 2. Both files are created even when ls fails.
Check yourself Question 1 of 4
Concept Check Standard error recall

Which file descriptor number is standard error by convention?

Recap & Key Takeaways

Descriptors 0, 1, and 2 are standard input, output, and error, and redirections change which file each number refers to before the command runs.
ls /proc/self/fd lists the open descriptors of a process, with each link pointing at its file or device.
Redirections are applied left to right, so the order of 2>&1 and > changes where error messages go.
exec with a redirection changes descriptors for the current shell, and exec 3>&- closes them.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue