In Linux, redirection allows you to alter the default behavior of commands by changing where they get their input and send their output.
This is a powerful tool for manipulating data and automating tasks.
There are three main types of redirection:
- Input redirection (using <): This changes the source of a command’s input from the keyboard to a file.
- Output redirection (using >): This redirects the output of a command from the terminal to a file.
- Error redirection (using 2>): This redirects the errors generated by a command from the terminal to a file.
Some examples of how redirection can be used:
1. Save the output of the ls command to a file called file_list.txt:
ls > file_list.txt
2. Display the contents of a file called data.txt but omit any errors:
cat data.txt 2> errors.txt
3. Combine the output of two commands together:
cat file1.txt file2.txt > combined_file.txt
4. Append to a file instead of overwriting it:
ls >> file_list.txt