list directory contents
To list the files in the directory /bin
$ ls /bin alsacard alsaunmute arch awk basename bash catchgrp chmod chown cp cpio csh .....
To print the number of newlines, words and bytes in files
$ wc test.c 44 93 1179 test.c
where the number of newlines, words and bytes in test.c is 44, 93 and 1179 respectively
To print the number of newlines, -l option is used
$ wc -l test.c 44 test.c
Now suppose to find the count of newlines we write on the terminal
$ wc -l After writing press ctrl + d this will display the numberof lines 4
Thus wc can take input both from the terminal and from a file
3. grep :
print lines matching a pattern
To print the lines which contain the pattern ‘main’ in test.c
$ grep main test.c int main (int argc, char *argv)
Now if we wish to print the line number in addition to the above information, -n option is used
$ grep -n main test.c 25:int main (int argc, char *argv)
Just like wc, grep can also take input from the terminal
$ grep -n main we are writing some words on the terminal and using grep to find the line where we wrote main 4:line where we wrote main(^D)
grep as soon as it finds the pattern ‘main’ displays the corresponding line on the terminal.
4. ps :
report a snapshot of the current processes
To see the processes running in the current shell
$ ps PID TTY TIME CMD 593 pts/2 00:00:00 bash 11270 pts/2 00:00:00 ps
To see the details of all the processes run by all the users
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.1 2044 512 ? Ss Jan29 0:00 init  root 2 0.0 0.0 0 0 ? S Jan29 0:00 [migration/0] root 3 0.0 0.0 0 0 ? SN Jan29 0:00 [ksoftirqd/0] root 4 0.0 0.0 0 0 ? S Jan29 0:00 [watchdog/0] root 5 0.0 0.0 0 0 ? S< Jan29 0:00 [events/0] root 6 0.0 0.0 0 0 ? S< Jan29 0:00 [khelper] ...
Let’s see some examples to demonstrate piping
To see the number of files or subdirectories starting with the letter ‘a’ in a directory /bin
$ ls /bin/a* /bin/alsacard /bin/alsaunmute /bin/arch/bin /awk
It is quite clear that the number of files is four. Here we can manually count them, but suppose there are more entries. Let’s use wc for the purpose, wc -l can count the number of newlines. So we can redirect the output of ls to wc. (Note wc can take input from both the file and the terminal)
$ ls /bin/a* | wc -l4
Thus using piping we have achieved our purpose. The output of ls which has to be displayed on the terminal, has been redirected as an input to wc (which was expecting input from the terminal).
We can use more than one pipes. Suppose we wish to find the number of commands containg pattern ‘aw’
$ ls /bin/ | grep aw awk gawk igawk pgawk
The above only displays the commands containing the pattern ‘aw’. But we need the count. Let’s use wc and one more pipe
$ ls /bin/ | grep aw | wc -l4
Thus we can have more than one pipe to achieve a purpose
Suppose we wish to know the details of all the processes run by the user ‘root’
We can use ps to search for the process details and grep to search for the pattern ‘root
root 1 0.0 0.1 2044 512 ? Ss Jan29 0:00 init  root 2 0.0 0.0 0 0 ? S Jan29 0:00 [migration/0] root 3 0.0 0.0 0 0 ? SN Jan29 0:00 [ksoftirqd/0] root 4 0.0 0.0 0 0 ? S Jan29 0:00 [watchdog/0] root 5 0.0 0.0 0 0 ? S< Jan29 0:00 [events/0] root 6 0.0 0.0 0 0 ? S< Jan29 0:00 [khelper] root 7 0.0 0.0 0 0 ? S< Jan29 0:00 [kthread] ... abcd 12369 0.0 0.1 3892 656 pts/2 R+ 13:55 0:00 grep root
You would have noticed in the last line user ‘abcd’ is displayed, because of the pattern root in ‘grep root’.
Suppose we wish to know the details of a process like ‘firefox‘. We can use ps to find that. But instead of manually looking for the particular command in the long displayed list from ps, we can make use of the piping. grep can be used to search for a particular pattern. So the output of ps can be redirected to the grep command which can be used for searching the command.
$ ps -x | grep firefox
5100 ? S 0:00 /bin/sh /usr/lib/firefox-22.214.171.124/firefox -UILocale en-US 5122 ? S 0:00 /bin/sh /usr/lib/firefox-126.96.36.199/run-mozilla.sh /usr/lib/firefox-188.8.131.52/firefox-bin -UILocale en-US 5127 ? Sl 1:03 /usr/lib/firefox-184.108.40.206/firefox-bin -UILocale en-US 5284 pts1 S+ 0:00 grep firefox
Thus instead of designing a new command for searching a particular running process, we have utilized the concept of piping.