5.1. Simple redirections

5.1.1. What are standard input and standard output?

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 or a particular terminal window 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.

5.1.2. The redirection operators

5.1.2.1. 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 

WarningDon'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.

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

5.1.2.2. 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 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.

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.

5.1.2.3. 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 examine another file when using less:


mike:~> less --help | grep -i examine :e [file] Examine a new file. :n app1.html app2.html app3.html chap_01.html chap_02.html chap_03.html chap_04.html chap_05.html chap_06.html chap_07.html chap_08.html chap_09.html chap_10.html chap_11.html f35.html gloss.html i14033.html images index.html intro_01.html intro_02.html intro_03.html intro_04.html intro_05.html intro_06.html intro_07.html intro_08.html intro_09.html intro_10.html intro-linux.html sect_01_01.html sect_01_02.html sect_01_03.html sect_01_04.html sect_01_05.html sect_01_06.html sect_01_07.html sect_02_01.html sect_02_02.html sect_02_03.html sect_02_04.html sect_02_05.html sect_03_01.html sect_03_02.html sect_03_03.html sect_03_04.html sect_03_05.html sect_03_06.html sect_04_01.html sect_04_02.html sect_04_03.html sect_04_04.html sect_04_05.html sect_04_06.html sect_05_01.html sect_05_02.html sect_05_03.html sect_05_04.html sect_05_05.html sect_06_01.html sect_06_02.html sect_06_03.html sect_06_04.html sect_06_05.html sect_07_01.html sect_07_02.html sect_07_03.html sect_07_05.html sect_07_06.html sect_07_07.html sect_07_08.html sect_08_01.html sect_08_02.html sect_08_03.html sect_08_05.html sect_08_06.html sect_09_01.html sect_09_02.html sect_09_03.html sect_09_04.html sect_09_05.html sect_09_06.html sect_10_01.html sect_10_02.html sect_10_04.html sect_10_05.html sect_10_06.html sect_10_08.html sect_10_09.html sect_11_01.html sect_11_02_04.html sect_11_02_05.html sect_11_02.html sect_11_03.html sect_11_04.html x11802.html x11877.html x12120.html x12249.html Examine the (N-th) next file from the command line. :p app1.html app2.html app3.html chap_01.html chap_02.html chap_03.html chap_04.html chap_05.html chap_06.html chap_07.html chap_08.html chap_09.html chap_10.html chap_11.html f35.html gloss.html i14033.html images index.html intro_01.html intro_02.html intro_03.html intro_04.html intro_05.html intro_06.html intro_07.html intro_08.html intro_09.html intro_10.html intro-linux.html sect_01_01.html sect_01_02.html sect_01_03.html sect_01_04.html sect_01_05.html sect_01_06.html sect_01_07.html sect_02_01.html sect_02_02.html sect_02_03.html sect_02_04.html sect_02_05.html sect_03_01.html sect_03_02.html sect_03_03.html sect_03_04.html sect_03_05.html sect_03_06.html sect_04_01.html sect_04_02.html sect_04_03.html sect_04_04.html sect_04_05.html sect_04_06.html sect_05_01.html sect_05_02.html sect_05_03.html sect_05_04.html sect_05_05.html sect_06_01.html sect_06_02.html sect_06_03.html sect_06_04.html sect_06_05.html sect_07_01.html sect_07_02.html sect_07_03.html sect_07_05.html sect_07_06.html sect_07_07.html sect_07_08.html sect_08_01.html sect_08_02.html sect_08_03.html sect_08_05.html sect_08_06.html sect_09_01.html sect_09_02.html sect_09_03.html sect_09_04.html sect_09_05.html sect_09_06.html sect_10_01.html sect_10_02.html sect_10_04.html sect_10_05.html sect_10_06.html sect_10_08.html sect_10_09.html sect_11_01.html sect_11_02_04.html sect_11_02_05.html sect_11_02.html sect_11_03.html sect_11_04.html x11802.html x11877.html x12120.html x12249.html Examine the (N-th) previous file from the command line. :x app1.html app2.html app3.html chap_01.html chap_02.html chap_03.html chap_04.html chap_05.html chap_06.html chap_07.html chap_08.html chap_09.html chap_10.html chap_11.html f35.html gloss.html i14033.html images index.html intro_01.html intro_02.html intro_03.html intro_04.html intro_05.html intro_06.html intro_07.html intro_08.html intro_09.html intro_10.html intro-linux.html sect_01_01.html sect_01_02.html sect_01_03.html sect_01_04.html sect_01_05.html sect_01_06.html sect_01_07.html sect_02_01.html sect_02_02.html sect_02_03.html sect_02_04.html sect_02_05.html sect_03_01.html sect_03_02.html sect_03_03.html sect_03_04.html sect_03_05.html sect_03_06.html sect_04_01.html sect_04_02.html sect_04_03.html sect_04_04.html sect_04_05.html sect_04_06.html sect_05_01.html sect_05_02.html sect_05_03.html sect_05_04.html sect_05_05.html sect_06_01.html sect_06_02.html sect_06_03.html sect_06_04.html sect_06_05.html sect_07_01.html sect_07_02.html sect_07_03.html sect_07_05.html sect_07_06.html sect_07_07.html sect_07_08.html sect_08_01.html sect_08_02.html sect_08_03.html sect_08_05.html sect_08_06.html sect_09_01.html sect_09_02.html sect_09_03.html sect_09_04.html sect_09_05.html sect_09_06.html sect_10_01.html sect_10_02.html sect_10_04.html sect_10_05.html sect_10_06.html sect_10_08.html sect_10_09.html sect_11_01.html sect_11_02_04.html sect_11_02_05.html sect_11_02.html sect_11_03.html sect_11_04.html x11802.html x11877.html x12120.html x12249.html Examine the first (or N-th) file from the command line. 

The -i option is used for case-insensitive searches - remember that UNIX systems are very case-sensitive.

If you want to save output of this command for future reference, redirect the output to a file:


mike:~> less --help | grep -i examine > examine-files-in-less mike:~> cat examine-files-in-less :e [file] Examine a new file. :n app1.html app2.html app3.html chap_01.html chap_02.html chap_03.html chap_04.html chap_05.html chap_06.html chap_07.html chap_08.html chap_09.html chap_10.html chap_11.html f35.html gloss.html i14033.html images index.html intro_01.html intro_02.html intro_03.html intro_04.html intro_05.html intro_06.html intro_07.html intro_08.html intro_09.html intro_10.html intro-linux.html sect_01_01.html sect_01_02.html sect_01_03.html sect_01_04.html sect_01_05.html sect_01_06.html sect_01_07.html sect_02_01.html sect_02_02.html sect_02_03.html sect_02_04.html sect_02_05.html sect_03_01.html sect_03_02.html sect_03_03.html sect_03_04.html sect_03_05.html sect_03_06.html sect_04_01.html sect_04_02.html sect_04_03.html sect_04_04.html sect_04_05.html sect_04_06.html sect_05_01.html sect_05_02.html sect_05_03.html sect_05_04.html sect_05_05.html sect_06_01.html sect_06_02.html sect_06_03.html sect_06_04.html sect_06_05.html sect_07_01.html sect_07_02.html sect_07_03.html sect_07_05.html sect_07_06.html sect_07_07.html sect_07_08.html sect_08_01.html sect_08_02.html sect_08_03.html sect_08_05.html sect_08_06.html sect_09_01.html sect_09_02.html sect_09_03.html sect_09_04.html sect_09_05.html sect_09_06.html sect_10_01.html sect_10_02.html sect_10_04.html sect_10_05.html sect_10_06.html sect_10_08.html sect_10_09.html sect_11_01.html sect_11_02_04.html sect_11_02_05.html sect_11_02.html sect_11_03.html sect_11_04.html x11802.html x11877.html x12120.html x12249.html Examine the (N-th) next file from the command line. :p app1.html app2.html app3.html chap_01.html chap_02.html chap_03.html chap_04.html chap_05.html chap_06.html chap_07.html chap_08.html chap_09.html chap_10.html chap_11.html f35.html gloss.html i14033.html images index.html intro_01.html intro_02.html intro_03.html intro_04.html intro_05.html intro_06.html intro_07.html intro_08.html intro_09.html intro_10.html intro-linux.html sect_01_01.html sect_01_02.html sect_01_03.html sect_01_04.html sect_01_05.html sect_01_06.html sect_01_07.html sect_02_01.html sect_02_02.html sect_02_03.html sect_02_04.html sect_02_05.html sect_03_01.html sect_03_02.html sect_03_03.html sect_03_04.html sect_03_05.html sect_03_06.html sect_04_01.html sect_04_02.html sect_04_03.html sect_04_04.html sect_04_05.html sect_04_06.html sect_05_01.html sect_05_02.html sect_05_03.html sect_05_04.html sect_05_05.html sect_06_01.html sect_06_02.html sect_06_03.html sect_06_04.html sect_06_05.html sect_07_01.html sect_07_02.html sect_07_03.html sect_07_05.html sect_07_06.html sect_07_07.html sect_07_08.html sect_08_01.html sect_08_02.html sect_08_03.html sect_08_05.html sect_08_06.html sect_09_01.html sect_09_02.html sect_09_03.html sect_09_04.html sect_09_05.html sect_09_06.html sect_10_01.html sect_10_02.html sect_10_04.html sect_10_05.html sect_10_06.html sect_10_08.html sect_10_09.html sect_11_01.html sect_11_02_04.html sect_11_02_05.html sect_11_02.html sect_11_03.html sect_11_04.html x11802.html x11877.html x12120.html x12249.html Examine the (N-th) previous file from the command line. :x app1.html app2.html app3.html chap_01.html chap_02.html chap_03.html chap_04.html chap_05.html chap_06.html chap_07.html chap_08.html chap_09.html chap_10.html chap_11.html f35.html gloss.html i14033.html images index.html intro_01.html intro_02.html intro_03.html intro_04.html intro_05.html intro_06.html intro_07.html intro_08.html intro_09.html intro_10.html intro-linux.html sect_01_01.html sect_01_02.html sect_01_03.html sect_01_04.html sect_01_05.html sect_01_06.html sect_01_07.html sect_02_01.html sect_02_02.html sect_02_03.html sect_02_04.html sect_02_05.html sect_03_01.html sect_03_02.html sect_03_03.html sect_03_04.html sect_03_05.html sect_03_06.html sect_04_01.html sect_04_02.html sect_04_03.html sect_04_04.html sect_04_05.html sect_04_06.html sect_05_01.html sect_05_02.html sect_05_03.html sect_05_04.html sect_05_05.html sect_06_01.html sect_06_02.html sect_06_03.html sect_06_04.html sect_06_05.html sect_07_01.html sect_07_02.html sect_07_03.html sect_07_05.html sect_07_06.html sect_07_07.html sect_07_08.html sect_08_01.html sect_08_02.html sect_08_03.html sect_08_05.html sect_08_06.html sect_09_01.html sect_09_02.html sect_09_03.html sect_09_04.html sect_09_05.html sect_09_06.html sect_10_01.html sect_10_02.html sect_10_04.html sect_10_05.html sect_10_06.html sect_10_08.html sect_10_09.html sect_11_01.html sect_11_02_04.html sect_11_02_05.html sect_11_02.html sect_11_03.html sect_11_04.html x11802.html x11877.html x12120.html x12249.html Examine the first (or N-th) file from the command line. 

Output of one command can be piped into another command virtually as many times as you want, just as long as these commands would normally read input from standard input and write output to the standard output. Sometimes they don't, but then there may be special options that instruct these commands to behave according to the standard definitions; so read the documentation (man and Info pages) of the commands you use if you should encounter errors.

Again, make sure you don't use names of existing files that you still need. Redirecting output to existing files will replace the content of those files.

5.1.2.4. The >> operator

Instead of overwriting file data, you can also append text to an existing file using two subsequent greater-than signs:

Example:


mike:~> cat wishlist more money less work mike:~> date >> wishlist mike:~> cat 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 wishlist.