Reputation: 11
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
Reputation: 34324
Assumptions:
find/glob
code example but mentions regular expression
in the question; I'm going to assume a regex
solution is acceptableTextFile2.TXT.tXT
) we're only interested in the 'last' extension (ie, what follows the last period in the filename)A-Z
) following the last periodSample 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-periodsNOTES:
sort
is not required; sort
was added to make it easier to compare the 2 lists of filenamesfind
4.6.0macos
; 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 suggestedUpvotes: 1