Saving Your Output
System administrators (and other human beings too) see a lot of critical messages fly by on the computer screen. It’s often important to save these messages so you can scrutinize them later, or (all too often) send them to a friend who can figure out what went wrong. So, in this section, we’ll explain a little bit about redirection, a powerful feature provided by Unix shells. If you come from MS-DOS, you have probably seen a similar, but more limited, type of redirection.
If you put a greater-than sign (>) and a filename after any command, the output of the command will be sent to that file. For instance, to capture the output of ls, you can enter:
$ ls /usr/bin > ~/Binaries
A listing of /usr/bin will be stored in your home
directory in a file named Binaries. If
Binaries had already existed, the > would wipe
out what was there and replace it with the output of the
ls command.
Overwriting a current file is a common user error. If your shell is
csh or tcsh, you can prevent
overwriting with the command:
$ set noclobber
And in bash you can achieve the same effect by entering:
$ noclobber=1
It doesn't have to be 1; any value will have the same effect.
Another (and perhaps more useful) way to prevent overwriting is to
append new output. For instance, having saved a listing of
/usr/bin, suppose we now want to add the contents
of /bin to that file. We can append it to the end of the
Binaries file by specifying two greater-than
signs:
$ ls /bin >> ~/BinariesBecome an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access