By Erick M Aqqa
Unix based operating systems like Linux offer a unique approach to join two discrete commands, and generate a new command using the concept of pipe(lines). For example, consider command1|command2. Here, whatever output is generated by the first command becomes the standard input for the second command. We can develop more and more complex Unix command sequences by joining many commands while maintaining input output relationships.
Another more Linux specific example would be ls -l|grep “^d”. This command displays details of only directories of the current working directory, i.e. the output of the ‘ls -l’ command becomes the input to the grep command, which displays only those lines that start with ‘d’ (they are nothing but the details of the files).
ls -l | grep “^d” | wc -l
This command displays number of directories in the given file.
grep “bash$” / etc / passwd | wc -l
This command displays number of users of the machine whose default shell is bash.
cut -t “: “-f 3 / etc / passwd | sort – n | tail – l
This command displays a number which is the largest used UID number in the system. Here, cut command first extracts UID’s of all the users in the system from the /etc / passwd file, and the same becomes input to sort; which sorts these numbers in numerical order and sends to tail command as input which in turn displays the largest number (last one).
The ‘tee’ command is used to save intermediate results in a piping sequence. It accepts a set of filenames as arguments and sends its standard input to all these files while giving the same as standard output. Thus, use of this in piping sequence will not break up the pipe.
For example, if you want to save the details of the directories of the current working directory while knowing their using the above piping sequence we can use tee as follows. Here, the file xyz will have the details of the directories stored.
ls -l | grep “^d” |tee xyz | wc -l
The following piping sequence writes the number of directories into the file pqr while displaying the name on the screen.
ls -l | grep “^d” | tee xyz | wc -l |tee pqr
The cmp utility compares two files of any type and writes the results to the standard output. By default, cmp is silent if the files are the same. If they differ, the byte and line number at which the first difference occurred is reported.
Bytes and lines are numbered beginning with one.
For example, cmp file1 file2
comm is a command used to compare two sorted files line by line.
Compare sorted files LEFT_FILE and RIGHT_FILE line by line.
-1 suppresses lines that are unique to the left file.
-2 suppress files that are unique to the right file.
-3 suppress lines that appear in both the left file and the right file. For example, comm p1 p2.
A pipe thus helps connect a set of processes, so that the output of one becomes the input of another. It lets a user browse through a large amount of data in a convenient manner.
Linux2Aix is an upbeat Linux blog containing all the latest and the newest Linus news and how-to’s for both amateur and professional Linux lovers