Log In | Register | January 30, 2023


Linux - June 14, 2012

Use input-output redirection (>, >>, |, 2>, etc.)

There are 3 special types which denote special file descriptors.
1. Standard Input – 0
2. Standard Output – 1
3. Error – 2

Output Redirection >

Assume we want to redirect the output of a command such as ls to a file called output.txt.

ls > output.txt

You could also add any number of flags to ls and redirect all of its output directly to your file as well. For instance:

ls -lh > output.txt

The above command takes the output of ls -lh and sends the results to a file called output.txt. If the file does not exist it will be created. If the file already exists, then that file will be overwritten with the new contents.

Error Output Redirection 2>

Lets say that you wanted to output the contents for a file named test.txt and redirect it to the file output.txt. The problem is that if the file doesn’t exist you’ll get a error message displayed to you instead. To handle the errors and redirect them to a given file you could simply prepend the number 2 to the > symbol.

cat test.txt 2> output.txt

The above command would redirect any error found in the command to the output.txt file.

Now lets say we want all errors to go to a error.txt file and good data to redirect to output.txt. We could do that by the following example:

cat test.txt > ouput.txt 2> error.txt

The above command tries ouput the contents for a file named test.txt and redirect it to the output.txt file. If any error is returned in the process we then redirect those errors to error.txt.

If you would like to redirect both errors and the output for your command to the same file you could simply use:

cat test.txt &> output.txt

The above command will redirect all errors and standard output to the file named output.txt.

Input Redirection <

Input redirection can be used to insert a list of predefined answers to a program such as fdisk or even import data from an external file to the mail program. Although it is less used, it is still an important tool to be familiar with.

Lets say you wanted to send the contents of a file to a particular users mailbox. You could simply type

mail username < file.txt

You could also combine your input and output redirection symbols to get the output of a command and redirect it to another file for later viewing. For instance:

wc -l < test.txt > output.txt

The above command takes in the contents of test.txt to the wc command and outputs the number of lines in the document. After that process is done the output  is then redirected to the file output.txt.

Append Redirection >>

Lets assume that we want to add data to a file but not overwrite the file. We can do so with append redirection. Just like output redirection, if the file you are directing the contents to does not exist it will be automatically created.

uptime >> output.txt

The above command will output the details about how long your system has been running and then redirect the result to the output.txt file. If the file does not exist then it will be created. If the file does in fact exist then the result will be appended allowing you to keep an ongoing record.


Pipes are a form of redirection which allow you to chain together various commands. For instance lets say you wanted to view your startup messages in a paging view. You could simply pipe it to the less utility which would then allow you to page through all of your startup messages as such.

dmesg | less

We could also sort our messages alphabetically by chaining in the sort program as such:

dmesg | sort -f | less

The above command would now sort our dmesg results and then place it in a pager for easy viewing. The -f flag for sort simply sorts the data case insensitively.

We could also use piping to find out how many files we have in our current directory by doing the following:

ls -l | wc -l

The above outputs the contents of our directory in long format and then passes that data to our wc command which uses the -l flag for counting the number of lines. The output given from the command would then be the number of files in your current directory.

We could also mix and match our redirection tools to write the contents to a file. For instance, assume we need to get the number of files from a directory and save that count to a text file named num_files.txt. We could do this using the following.

ls -l | wc -l > num_files.txt

Post By: | FavoriteLoadingAdd to favorites


RHCSA Certification Study Guide | DevBlog.co
Monday, June 25, 2012

[...] Use input-output redirection (>, >>, |, 2>, etc.) [...]

Thursday, April 22, 2021

drug stores near me https://canadianeve21.com/ Avana

Leave a Comment

Need Help? Ask a Question

Ask anything you want from how to questions to debug. We're here to help.

You Must Be Logged In To Post A Question.

Log In or Register