output as input
To redirect the output of a command we use the symbol ">".
In this case, we will use a file named hello.txt, in which we want to insert a certain output ("Sun" in this case):
echo "Sun" > hello.txtSo if we print the content of the file in our command-line, the output will be the following:
cat hello.txt SunIf we want to append a certain output to an existing file, we just have to use ">>" symbols:
cat hello.txt Sun echo "Moon" >> hello.txt cat hello.txt Sun Moon
input as output
To redirect input from a file for a command, the symbol "<" is used.
echo < $(cat hello.txt) Sun MoonThis is particularly useful when chaining commands.
Chaining (or piping)Chaining, also called Piping because it works with the pipe symbol "|", takes the output of a certain command and feeds it to another command.
cat hello.txt | grep Mo Moon
A simple exampleNow let's say that we want to combine those commands in a more complex operation. Let's say we want to take the content of a certain file and put it into another file.
We want to sort the animals whose name begins with letter "d" from the file animals.txt and put it into d_animals.txt.
grep d < animals.txt > d_animals.txt cat d_animals.txt Deer Dog Dolphin ...