Monday 24 October 2016

Advanced redirection features

There are three types of I/O, 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).

ls>dirlist2>&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.

It is an indication that the number that follows is not a file name, but rather a location that the data stream is pointed to. The greater-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.

$ ls 2>temp

$ ls -l temp

-rw-rw-r-- 1 flash 0 Sept 7 12:58 temp

$ ls 2 >temp

ls: 2: No such file or directory


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 to perform both actions simultaneously.

This tool is usually called on through a pipe (|), as demonstrated in the example below:

$uptime | tee –a file1 file2

01:57:30 up 145 days, 6:57, 3 users, load average: 0.29, 0.29, 0.34

$ cat file2

Thu Jun 10 11:10:34 IST 2017

01:57:30 up 145 days, 6:57, 3 users, load average: 0.29, 0.29, 0.34

No comments:

Post a Comment