Exercise: Piping, Sorting, and Counting

Piping

The pipe operator (|) sends the output of the command to its left to the input of the command on its right, thereby chaining simple commands together to perform more complex processing than a single command can do. It is similar to the redirection operator but directs the output to the next command instead of a file. Most Linux commands will read from stdin and write to stdout instead of only using a file, so | can be a very useful tool.

Another useful bash command is history, which will print all the bash commands you've entered in the shell on the system to stdout (up to a maximum set by the system administrator). This can be very useful when you only remember one part of a command or forget exact syntax, but it will quickly become more daunting to search the output the longer you use the shell . You can use | combined with grep to quickly and easily search this output. First try just the history command to view the normal output, then try searching for the cat command, which we've used several times, like this:

As you can see, it is much easier to find specific past commands by this method. The numbers before the commands indicate the line number in the bash history file, which corresponds to when the command was entered, as long as your history has not been cleared. You can even redirect this output to a file by:

This file can then be searched using less, a text editor (covered later), or by the grep command. For example, you can use grep to search for "lincoln":

Here's another example. Say you wanted to identify the processes using bash; you could use ps -ef (which outputs all processes in full format) as input to grep like so:

$ ps -ef | grep bash

There are many ways to use |, as you have seen in these examples, but feel free to explore more options!

Sorting

The sort command sorts the content of a file or any stdin, and prints the sorted list to the screen.


$ cat temp.txt 
cherry
apple
x-ray
clock
orange
bananna

$ sort temp.txt
apple
bananna
cherry
clock
orange
x-ray

To see the sorted list in reverse order, use the -r option. sort -n will sort the output numerically rather than alphabetically.


$ cat temp2.txt
7
48
1
56
8
32

$ sort -nr temp2.txt
56
48
32
8
7
1

Note that the two options can be combined by -nr, and order does not matter unless there is an input for a particular option. If you were looking for a filename that began with a "w", you may try:

$ ls | sort -r
Counting

The wc command reads either stdin or a list of files and generates a few different count summaries:

  • numbers of lines (by counting the number of newline characters)
  • numbers of words
  • numbers of bytes

Using the file temp.txt from the previous example, we can use wc to count the lines, words, and bytes (or characters):

$ wc temp.txt 
 6  6 40 temp.txt

The output shows that there are 6 lines, 6 words, and 40 bytes (or characters) in the file temp.txt. You can also use the following options with wc to specify certain behavior:

  • Only display line count: -l
    $ wc -l temp.txt
    6 temp.txt
  • Only display word count: -w
    $ wc -w temp.txt
    6 temp.txt
  • Only display byte count: -c
    $ wc -c temp.txt
    40 temp.txt
    

You can pipe ls to wc -l to list the number of files in a directory:

$ ls | wc -l
 
©   Cornell University  |  Center for Advanced Computing  |  Copyright Statement  |  Inclusivity Statement