Kolam Ayer MakersLinux Foundations

File Descriptor

Core Idea

A file descriptor is a small integer handle a process uses for an open file, pipe, socket, terminal, or device.

Standard File Descriptors

Number Name Usual Meaning
0 stdin Input.
1 stdout Normal output.
2 stderr Error output.

Redirection Examples

command >out.txt
command 2>error.txt
command >combined.txt 2>&1
command 2>/dev/null

> is shorthand for redirecting file descriptor 1. 2> redirects file descriptor 2. 2>&1 makes stderr point where stdout points at that moment.

Why Order Matters

command >combined.txt 2>&1

This sends stdout to combined.txt, then sends stderr to the same destination.

command 2>&1 >out.txt

This first sends stderr to the old stdout, then sends stdout to out.txt. The result is different.

Inspecting Open Descriptors

For your current shell process:

ls -l /proc/$$/fd

$$ is the shell process id.

Common Confusions

Proof Check

Run a command that produces both normal output and an error, redirect stdout and stderr separately, then inspect both files.

Docs Pointers