1.5 Intermediate Commands

1.5 Intermediate Commands

The following sections describe the most essential intermediate Unix commands beyond the basics that you saw earlier in this chapter. There are many more commands where these come from; check out Appendix A for a large list of commands on your system.

1.5.1 grep

grep prints the lines from a file or input stream that match an expression. For example, if you want to print the lines in the /etc/passwd file that contain the text root , use this command:

 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 when in this multiple-file mode. For example, if you want to check on every file in /etc that contains 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, it prints all lines that don't match). There is also a more powerful variant called egrep .

grep understands patterns known as regular expressions that are grounded in computer science theory and are ubiquitous in Unix utilities. Regular expressions are more powerful than wildcard-style patterns, and they have a different syntax. The two most important things to remember about regular expressions are these:

  • .* to match any number of characters (like the * in wildcards)

  • . to match one arbitrary character

The grep(1) manual page contains a detailed description of regular expressions, but it can be a little difficult to read. To learn more, you can try Mastering Regular Expressions [Friedl], or look at the regular expressions chapter of Programming Perl [Wall]. If you like math and are interested in where these things come from, look up Introduction to Automata Theory, Languages, and Computation [Hopcroft].

1.5.2 more and less

When a command's output is long, it can scroll off the top of the screen, and it's annoying to use a scrollbar to view such output because you have to move your hands around. You sometimes may also want to look at a large text file without starting a text editor. Two standard commands for text navigation are more and less .

To page through a big file like /usr/dict/words , use a command such as more /usr/dict/words . When running more , you will see the contents of the file, one screenful at a time. You can press the space bar to go forward in the file and the b key to skip back one screenful. To quit more , type q .

The less command performs the same function as more , but it is far more powerful and widely used. Use less --help to get a good summary of its operations.

As you will learn in Section 1.14, you can send the output of nearly any program directly to another program's input, enabling operations such as this (try it to see what it does):

 grep ie /usr/dict/words  less 

1.5.3 pwd

This program's name stands for "print working directory," and the command outputs the current working directory. That's all that pwd does, but it's useful. Some Linux distributions set up accounts with the current working directory in the prompt, but you may wish to change that because the current working directory takes up a lot of space on a line.

1.5.4 diff

To see the differences between two text files, use diff :

 diff  file1 file2  

There are several options that can control the format of the output, such as -c , but the default output format is often the most comprehensible (for human beings, that is).

1.5.5 file

If you see a file and are unsure of its format, try using file to see if the system can guess, based on a large set of rules:

 file  file  

You may be surprised to see how much this innocent-looking command can do.

1.5.6 find

It's frustrating when you know that a certain file is in a directory tree somewhere, and you just don't know where. Run find to find file in dir :

 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 you know why you need the -name and -print options. The find command accepts wildcard characters, such as * , but you must enclose them in single quotes ( '*' ) to protect the wildcard characters from the shell's own wildcard features (recall from Section 1.4.4 that the shell expands wildcards before running commands).

1.5.7 head and tail

To quickly view a portion of a file or stream, use the head and tail commands. For example, head /etc/inittab shows the first ten lines of this system configuration file, and tail /etc/inittab shows the last ten lines. You can change the number of lines to print by using the - n option, where n is the number of lines you want to see. If you want to print lines starting at line n , use tail + n .

1.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 numeric order, use the -n option. The -r option reverses the order of the sort.

How Linux Works
How Linux Works: What Every Superuser Should Know
ISBN: 1593270356
EAN: 2147483647
Year: 2004
Pages: 189
Authors: Brian Ward

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net