video

Linux tip: How to use pipes

In today’s Linux tip, we’re going to look at pipes, which take the output of the command on the left side and pass it to the command on the right to use as input.

Similar
Comments
Hi, this is Sandra Henry-Stocker, author of the “Unix as a Second Language” blog on Network World.
In today’s Linux tip, we’re going to look at pipes – one of the features of Linux that makes the command line so compelling. A pipe is represented by a vertical bar (the character over the backslash on your keyboard). What it accomplishes is quite special. It takes the output of whatever command is on the left side of it and passes it to the command on the right to use as input. If we want to know how many users are logged in, but don’t care who they are, we can run a command like this that counts the lines in the who command output:
$ who | wc -l
12
A command using vertical bars can get fairly complicated. The command below, for example, uses five vertical bars and transforms the output of the history command (which is generally between 100 and 1,000 lines long) into a list of the ten most heavily used commands.
$ history | awk '{print $2}' | sort | uniq -c | sort -rn | head -10
Let’s take a look at each step in the command so that you can grasp what’s happening at each step. First, we use the history command to view a list of the most recently entered commands. How many commands you see depends on the size of your history buffer.
$ echo $HISTSIZE
1000
Next, we pass that output to an awk command that removes everything but the second piece of text on each line – the command itself (arguments are lost).
After that, we sort the list of commands. Then we reduce that list to a list displaying of each unique command and how many times it appeared in the list of commands. At the point we have an alphabetic listing of the commands – each preceded by how many times it was used.
Then we use the sort command to sort the list in reverse numerical order so that the commands we’ve used most often show at the top of the list. And, lastly, we chop the output down to just the 10 most heavily used commands.
I’ve always been impressed by how much processing one can do on the Linux command line with a string of commands and pipes. And, once you get a complicated pipe working just the way you want, you can turn it into an alias to make it easier to use:
$ alias top10="history | awk '{print \$2}' | sort | uniq -c | sort -rn | head -10"
After that, you can just type “top10” to see which commands you’re using most often.
That’s your 2-minute Linux tip for today. If you liked this video, please hit the like and share buttons. For more Linux tips, be sure to follow us on Facebook, Youtube and NetworkWorld.com.