A keyboard, monitor, mouse, printer ... are all files in Linux. Linux identifies each of the hardware with unique file descriptors that are associated with it.
Now this nomenclature has got its own advantages. The main one being you can use many command line tools to send, receive or manipulate data with these devices.
For example, my mouse has the file descriptor
/dev/input/miceassociated with it (yours may be different).
So if I want to see the output of the mouse on my screen, I just enter the command :
$ cat /dev/input/mice
... and then move the mouse to get characters on the terminal. Try it for yourselves.
In some cases, running the above command will scramble your terminal display. If that happens, you can get back your terminal by typing
All programs in Linux have access to 3 special files. They are -
- Standard input - 0
- Standard output - 1, and
- Error - 2
Where the numbers 0, 1, and 2 denote file descriptors.
In the previous example, the tool
catused standard output which by default is the screen or the console to display the output.
Redirecting output to other files
You can easily redirect input / output to any file other than the screen. This is achieved in Linux using input and output redirection symbols.
These symbols are as follows:
>- Output redirection
<- Input redirection
Using a combination of these symbols and the standard file descriptors you can achieve complex redirection tasks quite easily.
Suppose, I want to redirect the output of
lsto a text file instead of the console. I can use the output redirection symbol and do it as shown below.
$ ls -l myfile.txt > test.txt
The above command will redirect the output to the file -
test.txt. If the file 'test.txt' does not exist, then it is automatically created and the output of the command
ls -lis written to it.
This is assuming that there is a file called myfile.txt existing in my current directory.
Now lets see what happens when we execute the same command after deleting the file myfile.txt.
$ rm myfile.txt $ ls -l myfile.txt > test.txt ls: myfile.txt: No such file or directory -- ERROR
What happens is that
lsdoes not find the file named myfile.txt and displays an error on the console or terminal. Now here is the fun part.
You can also redirect the error generated above to another file instead of displaying on the console by using a combination of error file descriptor and output file redirection symbol as follows:
$ ls -l myfile.txt 2> test.txt
2>, you are telling the program to redirect any error (2) to the file test.txt.
|Two open terminals can be used to practice output redirection|
Examples of Redirection
I can give one practical purpose for this error redirection which I use on a regular basis. When I am searching for a file in the whole hard disk as a normal user, I get a lot of errors such as :
find: /file/path: Permission denied
In such situations I use the error redirection to weed out these error messages as follows:
# find / -iname \* 2> /dev/null
Now all the error messages are redirected to
/dev/nulldevice. Thus I can reduce a lot of clutter in my terminal.
/dev/nullis a special kind of file in that its size is always zero. Anything you write to that file will just disappear.
The opposite of this file is
/dev/zerowhich acts as an infinite source. You can use
/dev/zeroto create a file of any size - when creating a swap file for instance.
If you have a line printer
/dev/lp0connected to your Linux machine, you can send any output to the printer using output redirection. For example, printing the contents of a text file (testfile.txt) is simple.
$ cat testfile.txt > /dev/lp0
You use input redirection using the less-than symbol (
<) and it is usually used with a program which accepts user input from the keyboard.
A legendary use of input redirection that I have come across is mailing the contents of a text file to another user.
$ mail ravi < mail_contents.txt
Now with the advances in GUI, and also availability of good email clients, this method is seldom used.
Here is another example of input redirection ...
Suppose you want to find the exact number of lines, number of words and characters respectively in a text file and you want to simultaneously write it to another file.
You can do it using a combination of input and output redirection symbols as follows:
$ wc < my_text_file.txt > output_file.txt
What happens above is the contents of the file
my_text_file.txtare passed to the command
wcwhose output is in turn redirected to the file
Appending data to a file You can also use the
>>symbol instead of output redirection to append data to a file. For example,
$ cat - >> test.txt
... will append what ever you write to the file test.txt.
Hope you liked this short tutorial on input output redirection in Linux.