Redirection
Redirection
Redirection lets you pass the output of one command as input to another command or file. Combined with piping, it enables powerful one-liners.
Input/Output Redirection
Earlier you saw how cat can create files. When you run:
cat > filename
the terminal prompts for input and writes everything you enter to filename. This is output redirection.
echo
You can redirect other commands as well. For example, use echo to overwrite or append to a file:
echo "Hello Omega" > hello.txt
echo "Another line" >> hello.txt
The single > overwrites existing content or creates the file, while >> appends to the end. The following screenshot shows both operators in action:

sort
To redirect a file into a command, combine the < operator with an executable. For example, sort organizes text alphabetically:
sort < alpha.txt

You can combine input and output redirection to create new files:
sort < alpha.txt > ordered.txt

Piping
Pipes (|) chain commands so the output of one becomes the input of the next. Suppose names.txt contains one name per line. This command finds lines containing both a and j:
cat names.txt | grep a | grep j

catprints the file contents.grep afilters for lines containinga.grep jfilters the remaining lines down to those containingj.
Getting comfortable with pipes is essential for advanced CLI work. Continue to the Shell Scripting guide to automate repetitive command sequences.