Most Linux commands read input, such as a file or another attribute for the command, and write output. By default, input is being given with the keyboard, and output is displayed on your screen. Your keyboard is your "standard input" (stdin) device, and the screen is the "standard output" (stdout) device.
However, since Linux is a flexible system, these default settings don't necessarily have to be applied. The standard output, for example, on a heavily monitored server in a large environment may be a printer.
Sometimes you will want to put output of a command in a file, or you may want to issue another command on the output of one command. This is known as redirecting output. Redirection is done using either the ">" (greater-than symbol), or using the "|" (pipe) operator which sends the standard output of one command to another command as standard input.
As we saw before, the cat command concatenates files and puts them all together to the standard output. By redirecting this output to a file, this file name will be created - or overwritten if it already exists, so take care.
nancy:~>cat test1 some words nancy:~>cat test2 some other words nancy:~>cat test1 test2 > test3 nancy:~>cat test3 some words some other words |
Redirecting "nothing" to an existing file is equal to emptying the file:
[jean@saturnus ~/Documents] ls -l list -rw-rw-r-- 1 jean jean 117 Apr 2 18:09 list [jean@saturnus ~/Documents] > list [jean@saturnus ~/Documents] ls -l list -rw-rw-r-- 1 jean jean 0 Apr 4 12:01 list |
This process is called truncating. The same redirection to an unexisting file will create a new empty file with the given name:
[jean@saturnus ~] ls -l newlist ls: newlist: No such file or directory [jean@saturnus ~] > newlist [jean@saturnus ~] ls -l newlist -rw-rw-r-- 1 jean jean 0 Apr 4 12:05 newlist |
Chapter 7 gives some more examples on the use of this sort of redirection.
Some examples using piping of commands:
To mail a text file to somebody:
cat file | mail somebody@somewhere.com
To find a word within some text, display all lines matching pattern1, or exclude lines also matching pattern2 from being displayed:
grep pattern1 file | grep -v pattern2
To display output of a directory listing one page at a time:
ls -la | less
To find an entry in a directory:
ls -l | grep entry
In another case, you may want a file to be the input for a command that normally wouldn't accept a file as an option. This redirecting of input is done using the "<" (less-than symbol) operator.
Below is an example of sending a file to somebody, using input redirection.
andy:~>mail mike@somewhere.org < to_do |
If the user mike exists on the system, you don't need to type the full address. If you want to reach somebody on the Internet, enter the fully qualified address as an argument to mail.
The following example combines input and output redirection. The file text.txt is first checked for spelling mistakes, and the output is redirected to an error log file:
aspell < text.txt > error.log
![]() | Don't overwrite! |
|---|---|
Be careful not to overwrite existing (important) files when redirecting output. Many shells, including Bash, have a built-in feature to protect you from that risk: noclobber. See the Info pages for more information. In Bash, you would want to add the set -o noclobber command to your .bashrc configuration file in order to prevent accidental overwriting of files. |
The pipe command (|) will feed output of one program as input to another. This is a way of sending mail using redirection of output instead of redirection of input:
andy:~>cat to_do | mail mike |
The output of the piped command can then be piped into another command, just as long as these commands would normally read input from the standard input and put output to the standard output.
Instead of overwriting file data, you can also append text to an existing file.
Example:
mike:~>date > > today's_wishlist mike:~>cat today's_wishlist more money less work Thu Feb 28 20:23:07 CET 2002 |
The date command would normally put the last line on the screen; now it is appended to the file test3.
There are three types of output, which each have their own identifier, called a file descriptor:
standard input: 0
standard output: 1
standard error: 2
In the following descriptions, if the file descriptor number is omitted, and the first character of the redirection operator is <, the redirection refers to the standard input (file descriptor 0). If the first character of the redirection operator is >, the redirection refers to the standard output (file descriptor 1).
Some practical examples will make this more clear:
ls > dirlist 2>&1
will direct both standard output and standard error to the file dirlist, while the command
ls 2>&1 > dirlist
will only direct standard output to dirlist. This can be a useful option for programmers.
All this is explained in detail in the Bash Info pages.
When a program performs operations on input and writes the result to the standard output, it is called a filter. One of the most common uses of filters is to restructure output. We'll discuss a couple of the most important filters below.
grep scans the output line per line, searching for matching patterns. All lines containing the pattern will be printed to standard output. This behavior can be reversed using the -v option.
Some examples: suppose we want to know which files in a certain directory have been modified in February:
jenny:~>ls -la | grep Feb |
The grep command, like most commands, is case sensitive. Use the -i option to make no difference between upper and lower case. Again, all the information is contained in the Info pages.
The command sort arranges lines in alphabetical order by default:
thomas:~>cat people-I-like | sort Auntie Emmy Boyfriend Dad Grandma Mum My boss |
But there are many more things sort can do. Looking at the file size, for instance. With this command, directory content is sorted smallest files first, biggest files last:
ls -la | sort -nk 5
![]() | Old sort syntax |
|---|---|
You might obtain the same result with ls -la | sort +4n, but this is an old form which does not comply with the current standards. |
The sort command is also used in combination with the uniq program (or sort -u) to sort output and filter out double entries.