Redirection in Linux


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.

The redirection operators

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.

vineet:~> cat test1
Hello to world

vineet:~> cat test2
Bye to world

vineet:~> cat test1 test2 > test3

vineet:~> cat test3
Hello to world
Bye to world


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. InBash, 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:

vineet:~> ls -l list
-rw-rw-r--    1 root   root     117 Apr  2 18:09 list

vineet:~> > list

nancy:~> ls -l list
-rw-rw-r--    1 root   root       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:

vineet:~> ls -l newlist
ls: newlist: No such file or directory

vineet:~> > newlist

vinnet:~> ls -l newlist
-rw-rw-r--  1 vineet   vineet     0 Apr  4 12:05 newlist
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

 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:

vineet:~> less --help | grep -i examine
  :e [file]      Examine a new file.
  :n          *  Examine the (N-th) next file from the command line.
  :p          *  Examine the (N-th) previous file from the command line.
  :x          *  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:

vineet:~> less --help | grep -i examine > examine-files-in-less

vineet:~> cat examine-files-in-less
  :e [file]      Examine a new file.
  :n          *  Examine the (N-th) next file from the command line.
  :p          *  Examine the (N-th) previous file from the command line.
  :x          *  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.

 The >> operator

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

vineet:~> cat wishlist
more money
less work

vineet:~> date >> wishlist

vineet:~> cat wishlist
more money
less work
Tue Oct  9 17:06:55 IST 2012

The date command would normally put the last line on the screen; now it is appended to the file wishlist.



 Use of file descriptors

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).
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.
. The example below demonstrates this:

[vineet@vineet]$ ls 2> tmp

[vineet@vineet]$ ls -l tmp
-rw-rw-r--  1 vineet vineet 0 Sept  7 12:58 tmp

[vineet@vineet]$ ls 2 > tmp
ls: 2: No such file or directory
The first command that vineet executes is correct (eventhough 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.

 Examples

  Writing to output and files simultaneously

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:

[vineet@vineet ~]$ date | tee file1 file2
Tue Oct  9 17:12:17 IST 2012
[vineet@vineet ~]$ cat file1
Tue Oct  9 17:12:17 IST 2012
[vineet@vineet ~]$ cat file2
Tue Oct  9 17:12:17 IST 2012
[vineet@vineet ~]$ uptime | tee -a file2 17:13:41 up 36 min, 2 users, load average: 0.27, 0.12, 0.14 [vineet@vineet ~]$ cat file2
Tue Oct  9 17:12:17 IST 2012
17:13:41 up 36 min, 2 users, load average: 0.27, 0.12, 0.14

No comments:

Post a Comment