Thoughts on Programming

July 24, 2011

Piping and Redirection in Linux

Filed under: Linux — shadiyya @ 6:07 pm

With pipes and redirection, we can “chain” multiple programs to create extremely powerful commands. Most programs on the command-line accept different modes of operation. Many can read and write to files for data, and most can accept standard input or output. This means that we can direct the output of one program as input to another program. We can then take the output of the second program and redirect it as input to yet another program, or redirect the output to a file.

A pipe is used to to send the output of one program to another program for further processing. With pipes, we can combine multiple commands together in a powerful way. The general syntax for pipes is:

$ command_1 | command_2 [| command_3 . . . ]

This chain can continue for any number of commands or programs.

The following is one of the most common ways of using pipes:

$ ls | less

As another example, suppose we have a text file called applist.txt and we want to find out what lines of it contain the word “desktop”. It’s easy with pipes. We list the contents of the file applist.txt and send the results to grep, which then filters all lines containing the desired word “desktop”, and displays those lines on your screen:

$ cat applist.txt | grep desktop

This direct connection between programs allows them to operate simultaneously and permits data to be transferred between them continuously rather than having to pass it through temporary text files or through the display screen and having to wait for one program to be completed before the next program begins.

Redirection is the transferring of standard output to some other destination, such as another program, a file or a printer, instead of the display monitor (which is its default destination). Standard output, sometimes abbreviated stdout, is the destination of the output from command line programs in Unix-like operating systems.

For example, to redirect the output to a file, we can use the > character like this:

$ ls > dir_listing.txt

The above redirects the output of the ls command to a file called dir_listing.txt. Because the output is redirected to a file, we won’t see any results of ls on your screen.

Each time we repeat the above command, the file dir_listing.txt is overwritten with the results of the ls command. If we want to append the new results to the file instead of rewriting it, we can use >> instead.

$ ls >> dir_listing.txt

Each time we repeat the above command, the new output of ls is added at the end of the dir_listing.txt file instead of overwriting the file.

The following adds the contents of File1 at the end of File2:

$ cat File1 >> File2

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: