In this section, we will take a look at IO Redirection
.
There are three data streams created when you launch a linux commnad.
With IO Redirection, the STDIN, STDOUT and STDERR can be redirected to a text file.
To redirect STDOUT to a file instead of printing it on the screen.
$ echo $SHELL > shell.txt
To append STDOUT to an exisiting file
$ echo $SHELL >> shell.txt
To redirect just the ERROR message we need to use 2
followed by forward arrow >
symbol and then the name of the filename in which the errors are written.
$ cat missing_file 2> error.txt
To append the STDERR to the exisiting file
$ cat missing_file 2>> error.txt
If you want to execute and not print ERROR messages on the screen even if it generates a standard ERROR. You can redirect to /dev/null
$ cat missing_file 2> /dev/null
Command Line Pipes allow the linking of multiple commands.
The pipes are defined using vertical bar symbol ( | ). |
$ grep Hello sample.txt | less
Another command to work with STDIN and STDOUT is the tee
command.
(|)
followed by tee
command.
$ echo $SHELL | tee shell.txt
tee
with -a option, to append instead of overwritting it
$ echo "This is the bash shell" | tee -a