Home | Login and Logout | Basic Command Line Commands | File System | File Editing Through Terminal | Java Programs | Input/Output Redirection | Grep | Works Cited
The purpose of input/output redirection is changing where different commands or programs will get their input from or where the output will go to. By default commands will read or write to your terminal window, however, it is extremely simple to change this.
Input redirection allows commands to take input from places other than the default terminal.
The notation for input redirection in terminal is the less than (
The format for input redirection command will look like "
[some command] < [input location]".
An easy example application of input redirection is sending emails through the linux command line. I have written an email to myself with the contents inside of a file titled "email.txt".
To email myself this email, I would type the following into console: "
mail email@example.com < email.txt".
This will use the supplied file "email.txt" as input for the command, and send the email.
The email will then look like the following image when delivered to the address specified.
Output redirection allows you to print command output to places other than the default terminal.
The notation for output redirection is similiar to that of input redirection. To direct the output away from the terminal
and into some other file/place, use the greater than (
>) symbol. The general format for output redirection will look
[some command] > [output file]". This will generate the file with the name specified in the directory
specified in [output file]. If instead of generating a new file, you would like to append the output to the end of another
file, replace the "
>" symbol with "
As an example, let's look at a simple java program that counts from 0 to 9. The default output from this program will look like the following screenshot.
This floods the terminal by a fair bit. Instead of having the output fill up the terminal window, we can redirect the output to some file that we can read later on. Let's redirect the output of the "Count" program to a text file titled "count.txt" instead of the terminal. To do this, we would type "
java Count > count.txt" into the terminal.
Now when we run this program, no output will be visible as it has been sent to the text file. We may open the
text file to observe that in fact, the output did end up there. This is evident in the following screenshots.
(Tutorials Point, n.d.)
Written by: Daniel Amusin, Derek DeCramer, Nikhil Thammadi