The <
character is used for input redirection. It instructs the shell to read input from a file.
Example: command < file
This tells the shell to execute command
on file
.
The >
character is used for output redirection. It instructs the shell to send the output of a command to a file.
Example: command > file
This tells the shell to execute command
and then write the output to file
.
The 2>
character is used for error redirection. It instructs the shell to send the error output of a command to a file.
Example: command 2> file
This tells the shell to execute command
and then write any error messages to file
.
The >>
operator is used to append the output of a command to a file instead of overwriting the file.
Example: command >> file
This tells the shell to execute command
and then append the output to file
.
# Input Redirection
sort < file.txt
# This command sorts the contents of file.txt.
# Output Redirection
ls -l > file.txt
# This command writes the long listing of the current directory to file.txt.
# Error Redirection
ls -l non_existent_directory 2> error.txt
# This command tries to list a non-existent directory and writes the error message to error.txt.
# Append Operator
echo "Hello, World!" >> greetings.txt
# This command appends "Hello, World!" to greetings.txt.
<
, >
, 2>
, and >>
operators for redirection.|
), which allow for chaining commands and redirecting output from one command as input to another.# Exercise 1
sort < file.txt > sorted_file.txt
# Exercise 2
wc -l < file.txt >> line_count.txt
# Exercise 3
ls -l non_existent_directory 2> error.txt