home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 15.9 nom: List Files that Don't Match a Wildcard Chapter 15
Wildcards
Next: 16. Where Did I Put That?
 

15.10 Wildcards that Match Only Directories

It's not news that the shell turns .* (dot asterisk) into every name in the current directory that starts with a dot: .login , .profile , .bin (I name my directory that way), and so on - including . and .. too.

Also, many people know that the shell turns */.* into a list of the dot files in subdirectories: foo/.exrc , foo/.hidden , bar/.xxx -as well as foo/. , foo/.. , bar/. , and bar/.. , too. (If that surprises you, look at the wildcard pattern closely - or try it on your account with the echo command: echo  */.* .)

What if you're trying to match just the subdirectory names, but not the files in them? The most direct way is: */. -that matches foo/. , bar/. , and so on. The dot (. ) entry in each directory is a link to the directory itself (18.2 , 14.4 ) , so you can use it wherever you use the directory name. For example, to get a list of the names of your subdirectories, type:

$ ls -d */.


bar/.       foo/.

(The -d option (16.8 ) tells ls to list the names of directories, not their contents.) With some C shells (but not all), you don't need the trailing dot (. ):

% ls -d */


bar/       foo/

(The shell passes the slashes (/ ) to ls . So, if you use the ls -F option (16.12 ) to put a slash after directory names, the listing will show two slashes after each directory name.)

When matching directory names that start with a dot, the shells expand the .*/ or .*/. and pass the result to ls -so you really don't need the ls -a option (16.11 ) . The -a is useful only when you ask ls (not the shell) to read a directory and list the entries in it. You don't have to use ls , of course. The echo (8.6 ) command will show the same list more simply.

Here's another example: a Bourne shell loop that runs a command in each subdirectory of your home directory:

for dir in $HOME/*/.
do
    cd $dir
    ...Do something
...
done

That doesn't take care of subdirectories whose names begin with a dot, like my .bin -but article 15.5 shows a way to do that too.

Article 21.12 shows a related trick that doesn't involve the shell or wildcards: making a pathname that will match only a directory.

- JP


Previous: 15.9 nom: List Files that Don't Match a Wildcard UNIX Power Tools Next: 16. Where Did I Put That?
15.9 nom: List Files that Don't Match a Wildcard Book Index 16. Where Did I Put That?

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System