< Previous | Contents | Next >
Pathname Expansion
The mechanism by which wildcards work is called pathname expansion. If we try some of the techniques that we employed in our earlier chapters, we will see that they are really expansions. Given a home directory that looks like this:
[me@linuxbox ~]$ ls | ||
Desktop ls-output.txt | Pictures | Templates |
Documents Music | Public | Videos |
we could carry out the following expansions:
[me@linuxbox ~]$ echo D*
Desktop Documents
[me@linuxbox ~]$ echo D*
Desktop Documents
and:
[me@linuxbox ~]$ echo *s
Documents Pictures Templates Videos
[me@linuxbox ~]$ echo *s
Documents Pictures Templates Videos
or even:
[me@linuxbox ~]$ echo [[:upper:]]*
Desktop Documents Music Pictures Public Templates Videos
[me@linuxbox ~]$ echo [[:upper:]]*
Desktop Documents Music Pictures Public Templates Videos
and looking beyond our home directory:
[me@linuxbox ~]$ echo /usr/*/share
/usr/kerberos/share /usr/local/share
[me@linuxbox ~]$ echo /usr/*/share
/usr/kerberos/share /usr/local/share
Pathname Expansion Of Hidden Files
As we know, filenames that begin with a period character are hidden. Pathname expansion also respects this behavior. An expansion such as:
echo *
does not reveal hidden files.
It might appear at first glance that we could include hidden files in an expansion by starting the pattern with a leading period, like this:
echo .*
It almost works. However, if we examine the results closely, we will see that the names “.” and “..” will also appear in the results. Since these names refer to the current working directory and its parent directory, using this pattern will likely produce an incorrect result. We can see this if we try the command:
ls -d .* | less
To better perform pathname expansion in this situation, we have to employ a more specific pattern:
echo .[!.]*
This pattern expands into every filename that begins with a period, does not in- clude a second period, and followed by any other characters. This will work cor- rectly with most hidden files (though it still won't include filenames with multiple leading periods). The ls command with the -A option (“almost all”) will provide a correct listing of hidden files:
ls -A