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


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 15.8 Getting a List of Non-Matching Files with grep -c Chapter 15
Wildcards
Next: 15.10 Wildcards that Match Only Directories
 

15.9 nom: List Files that Don't Match a Wildcard

The nom (no match) script takes filenames (usually, expanded by the shell) from its command line. It outputs all filenames in the current directory that don't match. As article 15.2 explains, ksh has a ! operator that works like nom , and tcsh has ^ pattern , but other shells don't. Here are some examples of nom :

  • To get the names of all files that don't end with .ms :

    % nom *.ms
    
    

  • To edit all files whose names don't have any lowercase letters, use command substitution (9.16 ) :

    % vi `nom *[a-z]*`
    
    

  • To copy all files to a directory named Backup (except Backup itself):

    % cp `nom Backup` Backup
    
    

Here's the script:




trap
 


case
 $*
 







comm
 -
 

#! /bin/sh
temp=/tmp/NOM$$
stat=1  # ERROR EXIT STATUS (SET TO 0 BEFORE NORMAL EXIT)
trap 'rm -f $temp; exit $stat' 0 1 2 15

# MUST HAVE AT LEAST ONE ARGUMENT.  ALL MUST BE IN CURRENT DIRECTORY:
case "$*" in
"") echo Usage: `basename $0` pattern 1>&2; exit ;;
*/*) echo "`basename $0` quitting: I can't handle '/'s." 1>&2; exit ;;
esac

# GET NAMES WE DON'T WANT TO MATCH; REPLACE BLANKS WITH NEWLINES:
echo "$*" | tr ' ' '\012' | sort > $temp
# COMPARE TO CURRENT DIRECTORY (-1 = ONE NAME PER LINE); OUTPUT NAMES WE WANT:
ls -1 | comm -23 - $temp
stat=0

You can remove the -1 option on the script's ls command line if your version of ls lists one filename per line by default; almost all versions of ls do that when they're writing into a pipe. Note that nom doesn't know about files whose names begin with a dot (. ); you can change that if you'd like by adding the ls -A option (uppercase letter "A", which isn't on all versions of ls ).

The script line with tr (35.11 ) will split filenames containing space characters. You can replace that line with the following three lines; they run more slowly on some shells but will fix this (unlikely) problem:

for
 

done |
 
for file
do echo "$file"
done | sort > $temp

- JP


Previous: 15.8 Getting a List of Non-Matching Files with grep -c UNIX Power Tools Next: 15.10 Wildcards that Match Only Directories
15.8 Getting a List of Non-Matching Files with grep -c Book Index 15.10 Wildcards that Match Only Directories

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