JimR
JimR

Reputation: 513

Find list of files that do not contain lower-case

Tried a couple of dozen iterations of this, but can't seem to find the right combination. I am using find to create a list of subdirectories and files. I want to omit any filenames that contain lower case characters. I am piping the output to awk to do further processing, so if it is easier there, that is fine. I've tried both the find and the awk find (separately). I am on Solaris 10 Unix if that makes any difference.

Input file list:

FOO/BAR.TXT
FOO/bar.TXT
FOO/BAT.TXT
BAZ/BAR.TXT
BAZ/baz.TXT

Desired output

FOO/BAR.TXT
FOO/BAT.TXT
BAZ/BAR.TXT

My find contains other exclusions, shown for example only

find ! \( -name 'FRED*' -o \
      -name '*JONES' -o \
      -name '*[a-z]8'  \) 

I've also tried moving the lower-case negation to a nawk find, such as this:

find ! \( -name 'FRED*' -o \
      -name '*JONES'  \) 
    | nawk  -F'/' '!/a-z/ {print $2}'

Every iterations I have tried either gives me no output, or else all output including the filename with lowercase characters.

Any help greatly appreciated.

Upvotes: 0

Views: 328

Answers (1)

John1024
John1024

Reputation: 113924

Let's consider a directory with these files:

$ find . -type f
./FOO/BAT.TXT
./FOO/bar.TXT
./FOO/BAR.TXT
./BAZ/BAR.TXT
./BAZ/baz.TXT

To exclude all the files with one or more lower-case characters in their names:

$ find . -regextype egrep -type f -regex '[^[:lower:]]*'
./FOO/BAT.TXT
./FOO/BAR.TXT
./BAZ/BAR.TXT

Notes:

  1. [:lower:] is the POSIX regex that matches any lower-case character. This is unicode-safe. [^[:lower:]] matches any character other than a lower-case letter. [^[:lower:]]* matches any sequence of characters that doesn't contain a lower case character.

  2. Find's -regex applies this regex to the whole path and insists that it match from beginning to end as if it were ^[^[:lower:]]*$.

  3. Find supports a wide-variety of regex styles. I chose the egrep style which includes the usual POSIX extended regular expressions.

Upvotes: 1

Related Questions