I’ve been playing around with redirection for the LBO course I’m following. I find what you can do with redirection fascinating (could be that I’m just easily amused). This is all command line work, so if you’re interested in honing your CLI-fu, read on. Some of this is really basic, some of it not so much.
NOTE: All of these commands should be executed with your regular user account. Don’t prefix the commands with sudo, don’t be operating as root.
First things first, you need to know a little about standard input, output and error. Standard input is usually your keyboard, standard out and standard error are usually your screen (or some window on your screen). Each of these 3 types of I/O (input/output) streams have an identifier associated with them called a file descriptor. In short, the file descriptor is an positive integer used to reference I/O.
* Standard input: 0
* Standard output: 1
* Standard error: 2
Before reading on, perform a man ls, man cat & man less to familiarize yourself with the commands we’ll be using.
A simple form of redirection can be illustrated with ls > dirlist. The greater than operator (>) redirects the results of ls to the file dirlist. If the file doesn’t exist when you execute the command, it will be created. If it does exist, it will be overwritten with the output from ls. After executing the command you can use cat dirlist |less to page through the contents of the file in your terminal. The cat command we issued to view the file uses another type of redirection that will be explained shortly.
In this example, you can append output to existing files with a double greater than (>>). If you want to keep a copy of your directory list with the time and date it was created appended to the bottom of the file you could execute date >> dirlist. Perform a cat dirlist to view the changes to the file.
In summary, using greater than (and double greater than), we’ve redirected standard output to a file instead of your screen. With the cat command we’ve redirected standard output from your screen to the input of another program (explained in the next section).
Before reading on, perform a man tee to familiarize yourself with the command we’ll be using.
You can also redirect the output from one program to the input of another. If you wanted to see the output of ls on screen as well as have it written to a file you could enter ls |tee dirlist. The vertical bar (referred to as pipe) character hands the output of ls to the program tee which writes its output to standard output (your screen) as well as a file.
The cat command back in step one worked the same way, the output from cat became the input for less and good ole’ less just spit its results to standard output and you got to read the file in pages.
So, it’s pretty simple to write output to files (> & >>), and it’s equally as simple to send output to other programs as input (|).
Here’s where the fun begins folks
Standard out and standard error are normally written to your screen, you would find any errors lines mixed in with non-error lines (this will be clear in a moment). We have the option of directing one or both streams to different locations. This is useful when performing a command that has gobs of results and we want to carefully review them. This is where the file descriptors for output and error come into play.
ls -R /etc/ 1> ~/goodnews 2> ~/badnews
This command performs a recursive directory listing of /etc redirecting the results of standard output (1>) to a file in your home directory named goodnews as well as redirecting standard error (2>) to a file named badnews. This command should return a few errors because of file permissions so execute the command and review the results. Pretty handy eh’?
Here’s my FAVORITE new toy. Not only can we send the results to files, but we can redirect output to other terminal sessions. Just for the joy of it, start 3 terminal sessions and arrange them so you can see them all at once. In each terminal, you will need to enter the command tty. The output will give you the device number of the current PTS (pts is a psuedo terminal). The results will look something like /dev/pts/x where x is a number. I’ll be using /dev/pts/8, 9 and 10.
In terminal that returned /dev/pts/8 I enter:
ls -R /etc/ 1> /dev/pts/9 2> /dev/pts/10
All of the non-error messages (standard output) are sent to /dev/pts/9 and all of the errors (standard error) are sent to /dev/pts/10. You can’t tell me that isn’t the coolest thing
.
These are just a few examples of redirection, there’s a lot more that you can do with it. Chapter 5 of the LBook on http://linuxbasics.org will give you more information and a series of useful exercises following the reading.