Datasheet
21.1.6 Less and More
Linux includes two small programs for viewing text files directly in the shell.
Rather than starting an editor to read a file like Readme.txt, simply enter less
Readme.txt to display the text in the console window. Use
Space to scroll
down one page. Use
Page Up and
Page Down to move forward or backward in
the text. To exit less, press
Q .
Instead of less, you can also use the older program more. However, it is less con-
venient because it does not allow you to scroll backwards.
The program less got its name from the the precept that less is more and can also
be used to view the output of commands in a convenient way. To see how this
works, read Pipes on the current page.
21.1.7 Pipes
Normally, the standard output in the shell is your screen or the console window
and the standard input is the keyboard. To forward the output of a command to
an application such as less, use a pipeline.
To view the files in the test directory, enter the command ls test | less.
The contents of the test directory will be displayed with less. This only makes
sense if the normal output with ls would be too lengthy. For instance, if you
view the contents of the dev directory with ls /dev, you will only see a small
portion in the window. View the entire list with ls /dev | less.
It is also possible to save the output of commands to a file. For example, ls
test > Content generates a new file called Content that contains a list of the
files and directories in test. View the file with less Content.
You can also use a file as the input for a command. For example, sort the text lines
in Testfile with sort < Testfile. The output of the command sort is sent
to the screen. The text is sorted by the first letters of the individual lines.
If you need a new file containing the sorted list, pipe the output of the command
sort to a file. To test this, create an unsorted name list in an editor and save it
under list in the test directory. Then change into test and enter the com-
mand sort < unsortedlist > sortedlist. Finally, view the sorted list
with less.
Just like the standard output, the standard error output is sent to the console as
well. However, to redirect the standard error output to a file named errors,
append 2> errors to the corresponding command. Both standard output
262 21.1. Introduction to Bash










