
Expansion
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
Tilde Expansion
As you may recall from our introduction to the cd command, the tilde character (“~”) has
a special meaning. When used at the beginning of a word, it expands into the name of the
home directory of the named user, or if no user is named, the home directory of the cur-
rent user:
[me@linuxbox ~]$ echo ~
/home/me
If user “foo” has an account, then:
[me@linuxbox ~]$ echo ~foo
/home/foo
69