Superbrka
Superbrka

Reputation: 11

Linux - How to write a glob pattern to that matches all file names with at least one uppercase character in an extension

How to write regular expression to find all files with at least 1 uppercase character (only) in file extension.

For example:

TextFile2.TXT.tXT
TextFile3.TXT.txt
TextFile.Txt
TextFile1.tXt
TextFile.TXT.txT

What's wrong with this 'find' command?

find . -type f -name "*.*[[:upper:]]*"

Output:

./TextFile2.TXT.tXT
./TextFile3.TXT.txt        (->this file shouldn't be here) 
./TextFile.Txt
./TextFile1.tXt
./TextFile.TXT.txT

Thanks !

Upvotes: 0

Views: 606

Answers (1)

markp-fuso
markp-fuso

Reputation: 34324

Assumptions:

  • OP has provided a find/glob code example but mentions regular expression in the question; I'm going to assume a regex solution is acceptable
  • for files that appear to have multiple extensions (eg, TextFile2.TXT.tXT) we're only interested in the 'last' extension (ie, what follows the last period in the filename)
  • files of interest must have at least 1 character following the last period
  • we're only interested in filenames with at least 1 capital letter (A-Z) following the last period

Sample files:

$ ls -1 | sort
TextFile.TXT.txT
TextFile.Txt
TextFile1.tXt
TextFile1.tXt.               # ignore this file
TextFile2.TXT.X
TextFile2.TXT.t              # ignore this file
TextFile2.TXT.tXT
TextFile3.TXT.txt            # ignore this file

One idea using find's -regex option:

$ find . -regex '.*TextFile.*[.][^.]*[A-Z]+[^.]*' | sort
./TextFile.TXT.txT
./TextFile.Txt
./TextFile1.tXt
./TextFile2.TXT.X
./TextFile2.TXT.tXT

Where:

  • .* - match anything prior to the string TextFile
  • .*[.] - match everything up to a period followed by ...
  • [^.]*[A-Z]+[^.]* - zero or more non-periods + at least one capital letter + zero or more non-periods

NOTES:

  • the sort is not required; sort was added to make it easier to compare the 2 lists of filenames
  • the above works with (GNU) find 4.6.0
  • question has been tagged with macos; this MacOS man page for 'find' appears to show support for the -regex option; I don't have access to a MacOS machine to verify if -regex works the same as suggested

Upvotes: 1

Related Questions