/dev/input/mice

$ cat /dev/input/mice

In some cases, running the above command will scramble your terminal display. If that happens, you can get back your terminal by typing reset

Standard input - 0 Standard output - 1, and Error - 2

cat

Redirecting output to other files

>

<

Output Redirection

ls

$ ls -l myfile.txt > test.txt

test.txt

ls -l

$ rm myfile.txt $ ls -l myfile.txt > test.txt ls: myfile.txt: No such file or directory -- ERROR

ls

$ ls -l myfile.txt 2> test.txt

By typing 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

find: /file/path: Permission denied

# find / -iname \* 2> /dev/null

/dev/null

/dev/null is 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/zero which acts as an infinite source. You can use /dev/zero to create a file of any size - when creating a swap file for instance.

/dev/lp0

$ cat testfile.txt > /dev/lp0

Input Redirection

<

$ mail ravi < mail_contents.txt

$ wc < my_text_file.txt > output_file.txt

my_text_file.txt

wc

output_file.txt

>>

$ cat - >> test.txt

Linux follows the philosophy that every thing is a file.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 descriptorassociated 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 :... and then move the mouse to get characters on the terminal. Try it for yourselves.All programs in Linux have access to 3 special files. They are -Where the numbers 0, 1, and 2 denote file descriptors.In the previous example, the toolused standard output which by default is the screen or the console to display the output.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 redirectionUsing 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 ofto a text file instead of the console. I can use the output redirection symbol and do it as shown below.The above command will redirect the output to the file -. If the file 'test.txt' does not exist, then it is automatically created and the output of the commandis 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.What happens is thatdoes 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: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 :In such situations I use the error redirection to weed out these error messages as follows:Now all the error messages are redirected todevice. Thus I can reduce a lot of clutter in my terminal.If you have a line printerconnected 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.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.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:What happens above is the contents of the fileare passed to the commandwhose output is in turn redirected to the fileAppending data to a file You can also use thesymbol instead of output redirection to append data to a file. For example,... will append what ever you write to the file test.txt.Hope you liked this short tutorial on input output redirection in Linux.