Linux And Command Line Redirection
One of the core tenets of Unix was that software applications should be small comprising of just what they needed to do their job. Rather than building new features into existing software, a new application should be created instead. These small applications would normally be designed to work together, with the output of one application being used as the input for another. This still holds true for most command line applications on modern Unix and Linux systems.
Command line applications generally have three input/output devices:
- The standard input (also called STDIN) – normally your keyboard.
- Standard output (STDOUT) – usually the screen.
- Standard Error (STDERR) – again, usually the screen.
When working with the Linux or Unix command line, redirection is the act of changing where these three inputs and outputs go.
First, and probably most commonly, is the pipeline, highlighted by the use of the pipe “|” character in the command. The pipe character is used to take the standard output of an application and pipe it into the standard input of another application. The use of pipelines enables you to put a number of applications together to effectively create far more complicated tools to get things done.
Next, we have specific output redirection. This is achieved using the “>” character. This is placed after an application’s command and it’s arguments. It means that the standard output will be redirected to the filename given after the symbol, for example:
ls -l > filelist.txt
This would put the output of the “ls -l” command into the file named filelist.txt. If it doesn’t exist it will be created, and if it does exist then any content in it will be erased and replaced. Any errors that occur will still go to the screen as the standard error isn’t being redirected.
Doubling up the symbol performs the same task only if the file exists and already contains data it will be kept and the new data will be written to the end of the file. For example:
ls -l >> filelist.txt
You can also prefix with an ampersand symbol to redirect both standard output and standard error to the provided file.
ls -l &> filelist.txt
Now we have a more special case: in addition to the names, each of the input and outputs has a number, 0 for stdin, 1 for stdout and 2 for stderr. Using these numbers you can also redirect the error output to standard output, or vice versa. So you may sometimes see our earlier example of writing both standard output and standard error to a file written like this:
ls -l > filelist.txt 2>&1
You can also use different files for output and errors such as:
ls -l > filelist.txt 2> errors.txt
In this case, the normal output goes to filelist.txt and any errors generated can be seen in errors.txt.
As well as output redirection we can do input redirection. Similar to output redirection, input redirection is used to send the contents of a file to the standard input of an application. Consider the following example:
iptables-restore < iptables.rules
The iptables-restore command will set up the iptables firewall rules with the rules given at the standard input. Input redirection allows the contents of the file iptables.rules to be used as this input, which is often combined with the iptables-save command to save and restore firewall rules.
Finally, we have a special command: the tee command. This command will write its input both to a file and to standard output. This is handy when you write a long command with a number of commands piped together to let you see what the output is partway through the chain. Because it’s a normal command you have to use the pipe before using tee:
ls -l | tee filelist.txt
You’ll see with this command that you can view the output on the screen as well as it is written to the file. With that, our Linux command line redirection overview is complete. If you are looking for more information, make sure to bookmark the VPS blog or follow us on Facebook and Twitter.