• Important: We recently upgraded our forum software - please let us know if you run into any issues.

LFCS – Input Output Redirection

Jarret B

Active Member
When running commands in a Terminal there are many ways information can be redirected. There are a few redirection conventions usable within a Terminal to make things easier.

Let’s look at a few of these redirection conventions:

  • Output
  • Append
  • Input
  • Error
  • Unnamed Pipe
  • Tee
  • Named Pipe
Output

An output pipe is used to send the standard output somewhere other than the screen. Typically, all output goes to the screen. We can direct he output to a file.

The output pipe is ‘Greater Than’ (>) symbol.

If, for instance, you wanted to create a file list of your HOME folder and place the list into a file called ‘Home-Folder.txt’ the command would be:

ls ~ > ~/Home-Folder.txt

If you look at the file in a text editor or even print it to the screen (cat ~/Home-Folder.txt) you should see a folder and file listing of your HOME folder.

If you wanted to simply create an empty file named ‘empty.txt’ in the HOME folder then you can use the command:

> ~/empty.txt

The output from nothing is placed into a file with the name ‘empty.txt’. If the file exists then it will be deleted and recreated as an empty file.

Not only does the Output Pipe create a file it can delete a file and create a new one. If you performed another output to the file ‘~/Home-Foler.txt’ the existing information will be deleted and the file recreated with the new output.

Any text output generated by a command can be saved to a file.

Append

If you did not want to delete all of the information in an existing file you can simply append, or add, new text to the existing text in a file.

To append and not overwrite the information use two ‘Greater Than’ (>>) symbols.

You could easily run the command ‘ls ~ >> ~/Home-Folder.txt’ again to add a second listing of the files and folders to a file. There would be no breaks to show where the one output ended and the next began. To insert a blank line you could echo a blank to the file with the command:

echo ‘ ‘ >> ~/Home-Folder.txt

Now you would have a blank line to designate a break when more data is appended to the file.

Input

Instead of sending the output of a command to a file or another command we can use the contents of a file for input. Instead of the ‘Greater Than’ symbol you turn it around and use the ‘Less Than’ (<) symbol.

Let’s assume we output a file listing of the HOME folder to the file called ‘Listing.txt’. If we wanted to use the file and count the number of entries we could use the command:

wc -l < Listing.txt

Of course, the redirect is not needed and you can specify the filename just as easily for the command to work. It does show how the command works though. Another example may be to copy a file and use the input and output redirector at the same time. For example, to copy the file ‘Listing.txt’ to ‘Listing1.txt’:

cat < Listing.txt > Listing1.txt

The contents of ‘Listing.txt’ are read in and instead of being displayed to the screen, standard output, it is sent to the file ‘Listing1.txt’.

As you can see, multiple pipes can be used together. Using multiple pipes can be very handy to accomplish what you may need.

Error

Commands within the Terminal may sometimes cause errors. Let’s say you have a Graphical User Interface (GUI) executable program which is not working properly. You may not be seeing errors on the screen, but this does not mean they aren’t occurring. Let’s assume the program is the HOME folder and call App1. Open a Terminal and make sure you are in the HOME folder. Type the following command:

./App1 2> App1-error.txt

The number ‘2’ represents the Standard Error (stderr) output. The ‘2’ will make the error output distinct from the standard output and capture only errors.

Now you can easily open the ‘App1-error.txt’ file and copy the error messages to search for them on the Internet to find possible solutions.

Unnamed Pipe

An unnamed Pipe is represented by a vertical bar (|). Basically the pipe is used for taking the output of the first command and using it in the second. For example, we want to count the number of icons on our Desktop. I can perform the following command:

ls ~/Desktop | wc -l

The listing of the files in the Desktop folder (items on our Desktop) are sent through the Word Count (wc) program and it counts the lines (-l). Each item on the Desktop is a single line output from the ‘ls’ command. The problem with the command is that it does not show the ‘Trash’ or any icons for mounted drives. These icons are from other places and not saved in the Desktop folder.

It is possible to use multiple pipes and use the output to get exactly what you want from the commands you use.

Tee

Sometimes you want the information to be placed in a file or used elsewhere, but still be shown on the screen.

Using the above example of listing the files in the HOME folder we can also show the output to the screen with the command:

ls ~ | tee ~/Home-Folder.txt

You can ‘tee’ the output to multiple files. For example, we can output the same command to the files File1, File2 and File3 with the command:

ls ~ | tee File1 File2 File3

More files can be listed as you need.

Named Pipe

A named pipe is different than what we have seen before. The named pipe is usually referred to as Inter-process Communication (IPC). The named pipe allows you to create a pipe with a specific name.

First, let’s create a pipe. A named pipe can have any valid name which we can name a file. Let’s name the pipe ‘Linux’. The command is:

mkfifo Linux

The folder where you create the pipe is where it will appear. It can be deleted later when you are done or you can leave for use later.

Now that the pipe is created we can use it.

In the Terminal type the following command:

ls > Linux

The cursor should move to the next line and stop with no prompt for a new command.

Do not close the Terminal, but open a new one. Once the new Terminal opens type the command:

cat < Linux

You should get the file listing created by the first Terminal shown in the second Terminal. The prompt in the first Terminal should be shown now. Multiple Named Pipes can be created and cross multiple Terminals. Once the data has been taken from Named Pipe it is empty, but still exists.

To delete a Named Pipe you can remove it like any file.

rm Linux

You should now have an understanding of Redirection in Linux and using pipes. Practice with the various pipes and find different ways to use them.
 

Members online

No members online now.


Top