Back to course

52. Input and Error Redirection

Linux Basics: From Zero to CLI Hero

Redirecting Input and Separating Errors

Input Redirection (<)

The < symbol redirects STDIN from a file instead of the keyboard. This is used when a command expects interactive input but you want to feed it pre-written data.

bash

The 'sort' command reads input from data.txt instead of the terminal

$ sort < data.txt

Error Redirection (2>) - Standard Error

Since STDERR has file descriptor 2, we use 2> to redirect error messages.

bash

Try to list a non-existent directory and redirect the error message

$ ls -l non_existent 2> errors.log

STDOUT (the successful list) goes to the screen,

but STDERR (the error message) goes to errors.log.

Redirecting Both STDOUT and STDERR

To redirect both successful output and errors to the same file, you can use the Bash shorthand &> or redirect FD 2 into FD 1.

bash

Redirect STDOUT and STDERR to all_output.log (Modern Bash)

$ ls -l /etc/ /nonexistent_dir &> all_output.log