Using Pipes
A little earlier we looked at alternative ways of using the more
and grep
commands by piping output through them using the |
operator. Pipes are such a useful device that
you will see them used in a variety of ways, often with multiple commands
piped together to produce a final output. For example, take a look at the
following composite command:
ps -ax | sort -k5 | less
The ps
program, in conjunction
with the argument -ax
, displays all the
background processes running on your computer, and is covered in more
detail in the section System Processes. On its own,
ps -ax
will display many screens full
of information; and the last few lines will look like Figure 7-12.
The processes are listed in order of when they were started (as
indicated by the numbers in the first column). So, to quickly search
through the list and find processes you may be interested in, you can use
the sort
command with the -k5
option. This causes a sort to occur on the
fifth column (as defined by the end of a section of whitespace), which is
the one containing the process path and filenames. This results in the
screen grab shown in Figure 7-13.
This is an improvement, but we can do even better by also adding the
less
command, which is an enhanced
version of more
. The less
command makes it easier to move back and
forth in the output because it supports the cursor keys as well as Enter
and the space bar.
So, the combined result is to list all the current processes alphabetically, displayed in a manner that ...
Get Ubuntu: Up and Running now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.