Unix 10
Unix 10
For example,
ls -l main.c
The shell provides a mechanism for generating a list of file names that match a pattern. For example,
ls -l *.c
generates, as arguments to ls, all file names in the current directory that end in .c. The character * is a pattern that will match any string including the null string. In general patterns are specified as follows. * Matches any string of characters including the null string. ? Matches any single character. [...] Matches any one of the characters enclosed. A pair of characters separated by a minus will match any character lexically between the pair. For example,
[a-z]*
matches all names in the current directory beginning with one of the letters a through z.
/usr/fred/test/?
matches all names in the directory /usr/fred/test that consist of a single character. If no file name is found that matches the pattern then the pattern is passed, unchanged, as an argument.
This mechanism is useful both to save typing and to select names according to some pattern. It may also be used to find files. For example,
echo /usr/fred/*/core
finds and prints the names of all core files in sub-directories of /usr/fred. (echo is a standard UNIX command that prints its arguments, separated by blanks.) This last feature can be expensive, requiring a scan of all sub-directories of /usr/fred.
There is one exception to the general rules given for patterns. The character `.' at the start of a file name must be explicitly matched.
echo *
will therefore echo all file names in the current directory not beginning with `.'.
echo .*
will echo all those file names that begin with `.'. This avoids inadvertent matching of the names `.' and `..' which mean `the current directory' and `the parent directory' respectively. (Notice that ls suppresses information for the files `.' and `..'.)
1.6 Quoting
Characters that have a special meaning to the shell, such as < > * ? | &, are called metacharacters. A complete list of metacharacters is given in appendix B. Any character preceded by a \ is quoted and loses its special meaning, if any. The \ is elided so that
echo \?
will echo a single \. To allow long strings to be continued over more than one line the sequence \newline is ignored.