0% found this document useful (0 votes)
40 views

Unix 10

The document discusses shell patterns and file name generation. It explains that the * character matches any string including the null string, ? matches any single character, and [] matches characters enclosed. Patterns like [a-z]* can be used to match file names beginning with letters a through z. The . character must be explicitly matched to include file names starting with ., as * would not normally match names starting with . or ..

Uploaded by

arunabhatla
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Unix 10

The document discusses shell patterns and file name generation. It explains that the * character matches any string including the null string, ? matches any single character, and [] matches characters enclosed. Patterns like [a-z]* can be used to match file names beginning with letters a through z. The . character must be explicitly matched to include file names starting with ., as * would not normally match names starting with . or ..

Uploaded by

arunabhatla
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Many commands accept arguments which are file names.

For example,
ls -l main.c

prints information relating to the file 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 ?, and


echo \\

will echo a single \. To allow long strings to be continued over more than one line the sequence \newline is ignored.

You might also like