Redirection (Streams)

In Linux, every program has three communication channels (streams) that it uses to interact with the environment:

1. Redirecting Output (>)

Use the > operator to save the output of a command into a file instead of displaying it on the screen.

Bash

ls > files_list.txt

Warning: The > operator overwrites the file if it already exists.

2. Appending Output (>>)

Use the >> operator to add (append) the output to the end of a file without deleting the existing content.

Bash

date >> updates.log

3. Redirecting Input (<)

Use the < operator to tell a program to read its input from a file instead of the keyboard.

Bash

wc -l < my_notes.txt

4. Redirecting Errors (2>)

Sometimes you want to separate normal output from error messages. Standard Error has the ID 2.

Bash

ls non_existent_file 2> error.log