2.5 Intermediate Commands
This section describes the most essential intermediate Unix commands.
2.5.1 grep
The grep command prints the lines from a file or input stream that match an expression. For example, to print the lines in the /etc/passwd file that contain the text root, enter this:
$ grep root /etc/passwd
The grep command is extraordinarily handy when operating on multiple files at once because it prints the filename in addition to the matching line. For example, if you want to check every file in /etc that contains the word root, you could use this command:
$ grep root /etc/*
Two of the most important grep options are -i (for case-insensitive matches) and -v (which inverts the search—that is, prints all lines that don’t match). There is also a more powerful variant called egrep (which is just a synonym for grep -E).
grep understands regular expressions, patterns that are grounded in computer science theory and are very common in Unix utilities. Regular expressions are more powerful than wildcard-style patterns, and they have a different syntax. There are three important things to remember about regular expressions:
.*matches any number of characters, including none (like the*in globs and wildcards)..+matches any one or more characters..matches exactly one arbitrary character.
The grep(1) manual page contains a detailed description of regular expressions, but it can be somewhat difficult to read. To learn more, you can read Mastering Regular Expressions, 3rd edition, by Jeffrey E. F. Friedl (O’Reilly, 2006), or see the regular expressions chapter of Programming Perl, 4th edition, by Tom Christensen et al. (O’Reilly, 2012). If you like math and are interested in where regular expressions come from, look up Introduction to Automata Theory, Languages, and Computation, 3rd edition, by Jeffrey Ullman and John Hopcroft (Prentice Hall, 2006).
2.5.2 less
The less command comes in handy when a file is really big or when a command’s output is long and scrolls off the top of the screen.
To page through a big file like /usr/share/dict/words, you can use the command less /usr/share/dict/words. When running less, you’ll see the contents of the file one screenful at a time. Press the spacebar to go forward in the file and press b (lowercase) to skip back one screenful. To quit, press q.
The less command is an enhanced version of an older program named more. Linux desktops and servers have less, but it’s not standard on many embedded systems and other Unix systems. If you ever run into a situation when you can’t use less, try more.
You can also search for text inside less. For example, to search forward for a word, you can type /word, and to search backward, you can use ?word. When you find a match, press n to continue searching.
As you’ll learn in Section 2.14, you can send the standard output of nearly any program directly to another program’s standard input. This is exceptionally useful when you have a command with a lot of output to sift through and you’d like to use something like less to view the output. Here’s an example of sending the output of a grep command to less:
$ grep ie /usr/share/dict/words | less
Try this command out for yourself. You’ll probably find many similar uses for less.
2.5.3 pwd
The pwd (print working directory) program simply outputs the name of the current working directory. You may be wondering why you need this when most Linux distributions set up user accounts with the current working directory in the prompt. There are two reasons.
First, not all prompts include the current working directory, especially because you may want to get rid of it in your own prompt because it takes up a lot of space. If you do so, you need pwd.
Second, the symbolic links that you’ll learn about in Section 2.17.2 can sometimes obscure the true full path of the current working directory. Use pwd -P to eliminate this confusion.
2.5.4 diff
To see the differences between two text files, use diff:
$ diff file1 file2
Several options can control the format of the output, and the default output format is often the most comprehensible for human beings. However, most programmers prefer the output from diff -u when they need to send the output to someone else, because automated tools have an easier time with this format.
2.5.5 file
If you see a file and are unsure of its format, try using the file command to see if the system can guess it:
$ file file
You may be surprised by how much this innocent-looking command can do.
2.5.6 find and locate
It’s frustrating when you know that a certain file is in a directory tree somewhere but you just don’t know where. Run find to find file in dir as follows:
$ find dir -name file -print
Like most programs in this section, find is capable of some fancy stuff. However, don’t try options such as -exec before you know the form shown here by heart and understand why you need the -name and -print options. The find command accepts special pattern-matching characters such as *, but you must enclose them in single quotes ('*') to protect the special characters from the shell’s own globbing feature. (Recall from Section 2.4.4 that the shell expands globs before running commands.)
Most systems also have a locate command for finding files. Rather than searching for a file in real time, locate searches an index that the system builds periodically. Searching with locate is much faster than find, but if the file you’re looking for is newer than the index, locate won’t find it.
2.5.7 head and tail
The head and tail commands allow you to quickly view a portion of a file or stream of data. For example, head /etc/passwd shows the first 10 lines of the password file, and tail /etc/passwd shows the last 10 lines.
To change the number of lines to display, use the -n option, where n is the number of lines you want to see (for example, head -5 /etc/passwd). To print lines starting at line n, use tail +n.
2.5.8 sort
The sort command quickly puts the lines of a text file in alphanumeric order. If the file’s lines start with numbers and you want to sort in numerical order, use the -n option. The -r option reverses the order of the sort.