There are three types of I/O, which each have their own identifier, called a file descriptor:
Also note that:
Some practical examples will make this more clear. Remember that the $ is not part of the command, but indicates the bash prompt:
First make a directory and a file:
$ mkdir testdir $ cd testdir $ touch file1 $ ls
Now try a command that will produce output with an error:
$ ls file1 file2 ls: file2: No such file or directory file1
Now redirect the output and the error to two different files:
$ ls file1 file2 1> dirlist 2> errorlist $ cat dirlist file1 $ cat errorlist ls: file2: No such file or directory
Notice that there is no output to the terminal with the ls command. The output has gone to the two files. The file listing went to dirlist being directed there as standard ouput (1>) and the error message went to errorlist being directed there as standard error (2>)
Now try redirecting both standard output and error to the same file:
$ ls file1 file2 > dirlist 2>&1 $ cat dirlist ls: file2: No such file or directory file1
Here we put the redirection symbols at the end, because bash will check this from the right to the left. Following is a shortcut way to do the same thing:
$ ls file1 file2 &> dirlist
Things are getting quite complicated here, don’t confuse the use of the ampersand here with the use of it in Section 4.1.2.1, where the ampersand is used to run a process in the background. Here, it merely serves as an indication that the number that follows is not a file name, but rather a location that the data stream is pointed to. Also note that the bigger-than sign should not be separated by spaces from the number of the file descriptor. If it would be separated, we would be pointing the output to a file again. The example below demonstrates this:
[nancy@asus /var/tmp]$ ls 2> tmp [nancy@asus /var/tmp]$ ls -l tmp -rw-rw-r-- 1 nancy nancy 0 Sept 7 12:58 tmp [nancy@asus /var/tmp]$ ls 2 > tmp ls: 2: No such file or directory
The first command that nancy executes is correct (even though no errors are generated and thus the file to which standard error is redirected is empty). The second command expects that 2 is a file name, which does not exist in this case, so an error is displayed.
All these features are explained in detail in the Bash Info pages.
If your process generates a lot of errors, this is a way to thoroughly examine them:
command 2>&1 | less
This is often used when creating new software using the make command, such as in:
andy:~/newsoft> make all 2>&1 | less --output ommitted--
Note that the shortcut &> will not substitute here. That shortcut only works when redirecting to a file.
Constructs like these are often used by programmers, so that output is displayed in one terminal window, and errors in another. Find out which pseudo terminal you are using issuing the tty command first:
andy:~/newsoft> make all 2> /dev/pts/7
You can use the tee command to copy input to standard output and one or more output files in one move. Using the -a option to tee results in appending input to the file(s). This command is useful if you want to both see and save output. The > and » operators do not allow you to perform both actions simultaneously.
This tool is usually called on through a pipe (|), as demonstrated in the example below:
mireille ~/test> date | tee file1 file2 Thu Jun 10 11:10:34 CEST 2004 mireille ~/test> cat file1 Thu Jun 10 11:10:34 CEST 2004 mireille ~/test> cat file2 Thu Jun 10 11:10:34 CEST 2004 mireille ~/test> uptime | tee -a file2 11:10:51 up 21 days, 21:21, 57 users, load average: 0.04, 0.16, 0.26 mireille ~/test> cat file2 Thu Jun 10 11:10:34 CEST 2004 11:10:51 up 21 days, 21:21, 57 users, load average: 0.04, 0.16, 0.26
Prev: Simple redirections
Home
Next: Filters
Old version for comparison
Copyright (c) by the authors.
This section of the wiki is licensed under the terms of the GNU Free Documentation License.
See the LBook-licensing page for details.
Linux® is a registered trademark of Linus Torvalds.