Pipe, as the name implies, is a sort of hollow tube (a pipe) where you can put data into one end and use the output on the other end. It takes the output from one command and uses it as the input for the following command. This functionality is only one of the many features that make linux the most powerful and useful operating systems you will ever use.

On most qwerty keyboards, the pipe command will be located above the backslash ( \ ) key.

To use the pipe command, you simply need to place this symbol ” | ” between commands. Here is a simple example:

root@host# ls /bin | grep tar

In this example, we are listing the contents of the /bin directory and then using the output of that response to search for the term “tar”. (yes, I know this is a poor example of searching, it’s an example g33kboy, get over it… : ) ). This command outputs the lines “tar” and “unicode_start”. Why unicode_start you ask? look at the middle three letters in start… hmmm s”tar”t.

Lets use a little more advanced command;

root@host [/usr]# du -mh | sort -nr | head -10
1020K ./local/cpanel/3rdparty/etc/ixed/ixed.lin
1020K ./local/Zend/lib/Optimizer_TS-3.3.3/php-4.4.x
1020K ./local/Zend/lib/Optimizer_TS-3.3.3/php-4.3.x
1016K ./local/cpanel/base/3rdparty/roundcube/program/lib/encoding
1012K ./share/mysql-test/suite/jp/t
1012K ./share/doc/initscripts-7.93.34
1012K ./local/cpanel/src/Cpanel-TaskQueue-0.3
1012K ./local/cpanel/src/3rdparty/gpl/libxml2-2.6.23/python
1008K ./share/doc/lm_sensors-2.8.7/doc
1008K ./local/cpanel/base/3rdparty/roundcube/program/lib/MDB2/Driver

This command will the send the output of the disk usage command to the sort command, which then sends the results to the head command which returns the results seen above. The flags or switches used after the commands themselves are used to modify the commands results in a specific way;

-n Sort a column in numerical order
-r Sort in reverse order (so “Z” starts the list instead of “A”)

and then to the head command which displays the first few lines at the top of a file. the -10 shows the first 10 lines of the results.

A more complex example would be something like;

grep "SMTP connection from" /var/log/exim_mainlog |grep "connection count" |awk '{print $7}' |cut -d ":" -f 1 |cut -d "[" -f 2 |cut -d "]" -f 1 |sort -n |uniq -c | sort -n

This series of piped commands searches the exim_mainlog and pulls all the IPs in all the entries and line them up in an ascending order. I am not going to go into all of the specific commands and switches themselves as that would be an article unto itself.

One of the commands I frequently use when verifying disk space usage is;

du -sk ./* | sort -nr | awk 'BEGIN{ pref[1]="K"; pref[2]="M"; pref[3]="G";} { total = total + $1; x = $1; y = 1; while( x > 1024 ) { x = (x + 1023)/1024; y++; } printf("%g%s\t%s\n",int(x*10)/10,pref[y],$2); } END { y = 1; while( total > 1024 ) { total = (total + 1023)/1024; y++; } printf("Total: %g%s\n",int(total*10)/10,pref[y]); }'

This is also a command which outputs the results of disk usage sorted very nicely.

As you can see the pipe command is very versatile when linking commands together to locate specific information, or modify information contained within files or folders. It is infinitely configurable when used with multiple commands and the related switches or flags.


I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.