Context: macOS Catalina (zsh)
This script is to process all JPEG files. This script does not process .JPG files, however it does process .jpg files.
top=/Users/user/Desktop/
for file in $top/**/*.jp*g(NDn.); do #selects filetypes: .jpg .jpeg
mogrify -auto-orient \
-gravity northWest \
-font "Arial-Bold-Italic" \
-pointsize 175 \
-fill red \
-annotate +30+30 $n \
-- $file &&
echo $file "was watermarked with" $n | tee -a forLooplog.txt
(( n++ ))
done
How can the second line be modified to be case insensitive and trap .JPG .JPEG files?
Best Answer
Especially for your case where the glob is
$top/**/*.jpg
, I would not turn thecaseglob
option off (same as turningnocaseglob
on¹) globally, as that affects all path components in glob patterns:See how it did find all the
jpg
andJPG
files in$top
(a
), but also the ones in an unrelated directory (A
) which just happened to have the same name but in upper case. Even if you don't have such directories, zsh will still look for them which means it needs to list the contents of every directory that makes up the components of$top
making that glob expansion more costly.IMO, that
nocaseglob
option is better left forgotten. It was only added to zsh for compatibility withbash
², and there most likely added to make the life of users of systems like Cygwin / macos that have case insensitive filesystem APIs easier.Instead, I'd used the
(#i)
glob operator (withextendedglob
) where you can specify which part of which glob should be case insensitive (similar to the~(i)
of ksh93):Or you can always do:
as you would in
sh
or any shell without case-insensitive glob operators.Also note that
*.jp(|e)g
instead of*.jp*g
which would match on filenames such asmy.jpeg.import.log
for instance.¹ or
CASEGLOB
,CASE_GLOB
,C_A_se_G_lob
, case and underscores are ignored in option names, and the support ofno
to turn an option off is to try and accommodate the mess with POSIX sh options (and other shells' including zsh itself) where some options are named with ano
prefix and some without for no immediately obvious reason.² though the bash behaviour is different (and preferable IMO, at least on systems with case sensitive file names) in this instance in that
a/*.jpg
would only findjpg
/JPG
files ina
, notA
as it only does case insensitive matching for path components that do have glob operators ([a]/*.jpg
would also find thejpg
/JPG
files inA
).