Installation guide

Chapter 11. Shell Prompt Basics 113
head can be a useful command, but because it is limited to the first several lines, you will not see how
long the file actually is. By default, you can only read the first ten lines of a file. You can change the
number of lines displayed by specifying a number option, as shown in the following command:
head -20
filename
11.11.2. The tail Command
The reverse of head is tail. Using tail, you can view the last ten lines of a file.
11.11.3. The grep Command
The grep command is useful for finding specific character strings in a file. For example, if you want
to find every reference made to "coffee" in the file sneakers.txt, you would type:
grep coffee sneakers.txt
You would see every line in that file where the word "coffee" is found.
Tip
Unless otherwise specified, grep searches are case sensitive. That means that searching for Coffee
is different than searching for coffee. So among grep’s options is -i, which allows you to make a
case-insensitive search through a file. Read the grep man page for more about this command.
11.11.4. I/O Redirection and Pipes
You can use pipes and output redirection when you want to store and/or print information to read at a
later time.
You can, for example, use grep to search for particular contents of a file, then have those results either
saved as a file or sent to a printer.
To print the information about references to "coffee" in sneakers.txt, for example, just type:
grep coffee sneakers.txt | lpr
11.11.5. Wildcards and Regular Expressions
What if you forget the name of the file you are looking for? Using wildcards or regular expressions,
you can perform actions on a file or files without knowing the complete filename. Just fill out what
you know, then substitute the remainder with a wildcard.
Tip
To read more about wildcards and regular expressions, take a look at the bash man page (man bash).
Remember that you can save the file to a text file by typing man bash | col -b bash.txt. Then,
you can open and read the file with less or pico (pico bash.txt). If you want to print the file, be
aware that it is quite long.