![](https://csdnimg.cn/release/download_crawler_static/9857714/bg14.jpg)
o *at* expands to all filenames that contain at.
If no files match a glob, the shell performs no expansion, and the command runs with literal characters such
as *. For example, try a command such as echo *dfkdsafh.
NOTE
If you’re used to MS-DOS, you might instinctively type *.* to match all files. Break this habit now.
In Linux and other versions of Unix, you must use * to match all files. In the Unix shell, *.*
matches only files and directories that contain the dot (.) character in their names. Unix filenames
do not need extensions and often do not carry them.
Another shell glob character, the question mark (?), instructs the shell to match exactly one arbitrary character.
For example, b?at matches boat and brat.
If you don’t want the shell to expand a glob in a command, enclose the glob in single quotes (''). For example,
the command echo '*' prints a star. You will find this handy for a few of the commands described in the
next section, such as grep and find. (You’ll learn more much about quoting in 11.2 Quoting and Literals.)
NOTE
It is important to remember that the shell performs expansions before running commands, and
only then. Therefore, if a * makes it to a command without expanding, the shell will do nothing
more with it; it’s up to the command to decide what it wants to do.
There is more to a modern shell’s pattern-matching capabilities, but * and ? are what you need to know now.
2.5 Intermediate Commands
The following sections describe 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 patterns known as regular expressions 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 two important things to remember about regular expressions:
o .* matches any number of characters (like the * in wildcards).
o . matches one arbitrary character.
NOTE
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 read Mastering Regular Expressions, 3rd edition
(O’Reilly, 2006), or see the regular expressions chapter of Programming Perl, 4th edition (O’Reilly,