Redirection

Output Redirection

This is a very critical concept to understand with Linux. While using and learning about Linux you will come across many commands that use redirection. You have file redirection and command redirection. With file redirection you are taking the output of the command and placing this within a file (either a new or appending data). With command redirection you are taking the output of one command and using it as the input as another command. You can do this many, many times!

File Redirection Operators

These are the more common file redirection operators.

Command Redirection

To do command redirection is a lot simpler as it just uses the pipe command (|). The pipe character links commands together where the output of one command becomes the input of another command. For example:

ls -a | wc

The ls -a command would be run, but you would not see the output from this command. The output with be used by the wc command as input and you'd get the results displayed of wc.

If we were to just run ls -a and our output was:

.

..

jdoodle.sh

If we then ran ls -a | wc our output would be:

3 3 16

This is what is generated as the output of the wc command.

Learning more about pipes by reading the following:


tee

The tee command is a great command to use if you want to store the output of a command to a file and also be able to view it on the command line (stdout).

https://linux.die.net/man/1/tee

https://linuxize.com/post/linux-tee-command/

xargs

The xargs command is very helpful when using pipes. The xargs command has the ability to take output of another command and pass it to another command as a argument. I know this sounds confusing, but please watch the video and look at the links below to gain a better understanding.

https://linux.die.net/man/1/xargs

https://www.tecmint.com/xargs-command-examples/