You are here: Home » Lifetime Academy » Piping Output from One Command to Another

Piping Output from One Command to Another

Piping allows the user to do several fantastic thing by combining utilities. I will cover only very basic uses for piping. I most commonly use the pipe command, |, to pipe text that is output from one command through the grep command to search for text.

See if a program, centericq, is running:$ ps ax | grep centericq
25824 pts/2 S 0:18 centericq
Count the number of files in a directory (nl counts things):$ ls | nl
1 #.emacs#
2 BitchX
3 Outcast double cd.lst
4 bm.shader
5 bmtexturesbase.pk3
If my memory serves using RPM to check if a package is installed:$ rpm -qa | grep package_name
A more advance example:$ cat /etc/passwd | awk -F: ‘{print $1 “t” $6}’ | sort > ./users
This sequence takes the information if the file passwd, pipes it to awk, which takes the first and sixth fields (the user name and home directory respectively), pipes these fields separated by a tab (“t”) to sort, which sorts the list alphabetically, and puts it into a file called users.


Leave a Reply

Your email address will not be published. Required fields are marked *