- [Instructor] Every command has one input
standard in and two outputs
standard out and standard error.
A pipe sends standard out from one command
to the standard in of another.
A redirect works in a similar manner,
but the source or destination is the file system.
By default the output of a command goes to the screen.
This includes standard out and standard error.
Standard out is the successful output from a command.
Standard error is used for error messages.
Redirection allows us to split
these two outputs individually.
We can redirect either output or both to a disk.
Commands also have a standard in.
If you wanted to redirect a file to a command
we can with the redirect toward the command.
Once the command has processed the output
we could then redirect that output back to the disk.
Let's see what this looks like using commands.
To redirect standard out from a command to a file
use a greater than symbol.
One greater than symbol overwrites a file
or creates it if it doesn't exist.
To append to a file
supply two greater than symbols.
This will also create the file if it doesn't exist.
If standard out is the first output
then standard error must be the second output.
To redirect standard error supply two greater than.
Again, this overwrites the file if it exists
or creates it if it doesn't.
To append to an existing file
redirect the standard error with two
greater than, greater than.
There are times when we might want to
redirect all output from a command to a file.
To do this we'll use ampersand greater than.
Note that if you redirect all output from a command
nothing will show up on the screen.
Once again, to append add an extra greater than symbol.
You can redirect both into the standard end of a command
and then back out to a file again.
For instance, we can redirect a file
into the sort command to sort it
and then redirect the sorted output
back out to a different file.
We won't be able to redirect
back over the original file, though, as it will conflict.
There are times when we might want to
send the output of a command to a file
and to the screen.
Neither pipes or redirects handle this natively,
but Linux provides a special command for doing this
called tee.
It splits or tees the output and sends it both to a file
and to the screen.
This example takes the output of LS
and saves it to a file names lsout.txt
as well as display it on the screen.
If we want to append to the lsout.txt file
we can add the -a or append option to tee.
Let's see how this works by using redirects in a terminal.
In a terminal type in
find /etc | sort | tee etcsort.txt | wc-l.
In this example we get a list of all files in etc
using the find command.
That list will send to the sort command using a pipe.
We'll split the output by using the tee command
to save a copy of the sorted list to a file
called etcsort.txt,
but also send it into the next pipe.
Lastly we'll use wc-l to count the lines.
Go ahead and hit enter.
What we should see when done
is the sorted lines in the etcsort.txt file
and the number of lines on the screen.
Notice the last line is the output from wc-l
and it is the number of lines in the file.
Also notice that we have more than just
the number of lines on the screen.
We have what looks like permission denied error messages
from the find command.
We can redirect standard error using the two
greater than symbols.
Let's add this.
Press the up arrow key
and move your cursor over to etc and insert
2> etcerr.txt.
You should have
find /etc 2> etcerror.txt | sort | tee etcsort.txt | wc -l
and hit enter.
Now the error messages from find
will be saved in etcerror.txt.
The non error message text will be sorted
and saved as etcsort.txt
and the total lines will be displayed on screen.
One last tip I'd like to share
is totally getting rid of output from a command
by redirecting it to a bottomless pit.
This bottomless pit is provided by /dev/null.
If we redirect output from commands
to /dev/null nothing will show on the screen.
For instance, type in
find /etc &> /dev/null
and hit enter.
All output disappeared.
Whenever we want to hid any output from a command
we can just redirect it to /dev/null.