HOWTO: Use the Pipe ‘|’ Symbol
The pipe symbol ( | which is usually produced by shifting the \ key) provides a useful way to link the output of several commands together. Sound complicated? It can be, but even some very rudimentary knowledge of the pipe symbol can produce some very useful results.
For example, reader Geoff recently commented about a command that he uses from time to time:
du -a | sort -rn | head -50
Which recursively sorts the largest 50 files in the current working directory.
This is an interesting command line because it’s actually three commands piped into each other. What this command actually does is:
- du: estimates the diskspace that the files in this directory are taking up (recursively, including every file in every sub directory)
- sends that list to the sort application which (via the -rn switches) is being told to sort du’s output in reverse numeric order
- sort’s output is then sent to the head command which is being told to display the first 50 of those files (technically, the head command displays the first 50 lines of a text file, but in this case it will take the first 50 lines of the output it is being fed, regardless of where it comes from)
Another area where I use the pipe symbol frequently is when I’m looking to see if a specific application is running. I can use the ps command to see what’s running (or stopped), but that usually results in a very long list of processes. If I want to find out if, say, sort is running, then I can pipe the results of the ps command to grep like so:
ps aux | grep sort
Currently, on my system, this results in the following output:
jon 16730 0.0 0.7 27212 3828 pts/4 T 11:44 0:00 sort -rn
jon 17147 0.0 0.0 1640 488 pts/4 S+ 11:50 0:00 grep sort
I can see that the text ’sort’ shows up twice in my running processes list and I can further see that the second instance of it is the command I just issued. Therefore, the first must be an actual instance of the sort application running.
Neat, or what?
Related Stories
POSTED IN: How To
1 opinion for HOWTO: Use the Pipe ‘|’ Symbol
New Linux User » HOWTO: Find Files With Specified Text In Them.
Feb 1, 2006 at 6:09 am
[…] Ok, that’s an ugly title, but you get the idea. If you want to search a big ugly text file (like a log, for example) for a certain string, a combination of grep and the pipe symbol can be a life saver. […]
Have an opinion? Leave a comment: