5.1.2. The redirection operators
188.8.131.52. Output redirection with > and |
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
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.
Redirecting "nothing" to an existing file is equal to emptying the file:
nancy:~> ls -l list -rw-rw-r-- 1 nancy nancy 117 Apr 2 18:09 list nancy:~> > list nancy:~> ls -l list -rw-rw-r-- 1 nancy nancy 0 Apr 4 12:01 list
This process is called truncating .
The same redirection to an nonexistent file will create a new empty file with the given name:
nancy:~> ls -l newlist ls: newlist: No such file or directory nancy:~> > newlist nancy:~> ls -l newlist -rw-rw-r-- 1 nancy nancy 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 find a word within some text, display all lines matching "pattern1" , and 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 a file in a directory:
ls -l | grep part_of_file_name
184.108.40.206. Input redirection
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 firstname.lastname@example.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 .
This reads a bit more difficult than the beginner's cat file | mail someone , but it is of course a much more elegant way of using the available tools.
220.127.116.11. Combining redirections
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:
spell < text.txt > error.log
The following command lists all commands that you can issue to