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


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 15.4 What if a Wildcard Doesn't Match? Chapter 15
Wildcards
Next: 15.6 Maybe You Shouldn't Use Wildcards in Pathnames
 

15.5 Matching All "Dot Files" with Wildcards

If you want to match all files in a directory whose names do not start with a dot ( . ), it's easy: just use an asterisk ( * ). But what about files that do start with a dot? That's harder because dot-asterisk ( .* ) matches the directory links named . and .. that are in every directory; you usually won't want to match those.

The Korn and some Bourne shells, as well as bash , let you use the sequence .[!.]* to match all dot files, where [!.] means "anything but a dot." tcsh understands .[^.]* instead.

Otherwise, what can you do? You can use .??* , which matches all filenames that start with a dot and have at least two characters, but that doesn't match filenames like .a with just one character after the dot. Here's the answer:

.[^A--0-^?]*

That expression matches all filenames whose second character is in the ASCII chart ( 51.3 ) but isn't a dot or a slash ( / ). The range starts with CTRL-a ( ^A is an actual CTRL-a character, not the two characters ^ and A ) and runs through a dash ( - ). Then it covers the range from zero ( 0 ) through DEL or CTRL- ? (make by pressing your DELETE or RUBOUT key; you may have to type CTRL-v or a backslash ( \ ) first).

Yuck - that's sort of complicated. To make it easy, I set that sequence in a shell variable named dots from my shell setup file ( 2.2 ) . Here are three versions; the third is for shells whose built-in echo doesn't understand \ nnn sequences:


set dots=".[`echo Y-0-Z | tr YZ \\001\\177`]"   
csh

dots=".[`echo \\\\001-0-\\\\0177`]*"   
sh, etc.

dots=".[`echo Y-0-Z | tr YZ \\001\\177`]*"   
sh with old echo

(The tr command in backquotes ( 9.16 ) turns the expression Y--0-Z into the range with CTRL-a and DEL that we want. That keeps ugly, unprintable characters out of the .cshrc file. See article 45.35 .) So, for example, I could move all files out of the current directory to another directory by typing:

% 

mv * $dots

 

/somedir

- JP


Previous: 15.4 What if a Wildcard Doesn't Match? UNIX Power Tools Next: 15.6 Maybe You Shouldn't Use Wildcards in Pathnames
15.4 What if a Wildcard Doesn't Match? Book Index 15.6 Maybe You Shouldn't Use Wildcards in Pathnames

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