What is the difference between "|", ">", and ">>"
At the most basic
command 1 | command 2 sends the output from the first command as the input for the second command (pipe reads from STDOUT and writes to STDIN).
cat file.txt | grep "tux" will print out the file, and then use it as the input for the
>> are basically aliases for the
tee command. The
tee command writes files (tee reads from STDIN and writes to STDOUT).
> is an alias for
| tee and
>> is an alias for
| tee --append
If I want to write the current files in my home directory to a file I would use
ls ~/ > files.txt The output of
ls is now stored in the file
I now want to add the contents of
/ into the same file. But using
> again will overwrite the file, it will always clear all data from a file and replace it with its own load.
To add text to the end of a file we need
ls / >> files.txt will result in the contents of
~/ being in the top of our file and the contents of
/ being below that.
>> are just aliases for a
| tee command, you cannot write to a file you do not have permission for with these. If you need
sudo to write a file, you will need to hand write out the full
tee command. So to add the text
Banner /etc/banner to the end of our
sshd_config file we will need to use the command
echo "Banner /etc/banner" | sudo tee --append /etc/ssh/sshd_config