On my Mac running Yosemite I have a directory of files on my Desktop:
29_foo10.bar
29_foo2.bar
29_foo20.bar
29_foo3.bar
I want to target the files with a single digit after foo
. When I use find -name
I can target the selection of files with:
USERNAME=darthvader
DIRECTORY="/Users/$USERNAME/desktop/test"
for theProblem in $(find $DIRECTORY -type f -name '29_foo[0-9].bar'); do
cd $DIRECTORY
echo $theProblem
done
and I'm returned the two files in the terminal (29_foo2.bar
& 29_foo3.bar
) but when I try using -regex
it returns nothing, code:
for theProblem in $(find $DIRECTORY -type f -regex '29_foo[0-9].bar'); do
cd $DIRECTORY
echo $theProblem
done
I did some research and found OS X Find in bash with regex digits \d not producing expected results
so I modified my code to:
for theProblem in $(find -E $DIRECTORY -iregex '29_foo[0-9].bar'); do
and I'm returned:
No such file or directory
so I tried:
for theProblem in $(find -E $DIRECTORY -type f -regex '29_foo[0-9].bar'); do
but I still get:
No such file or directory
So I did some further research and found bash: recursively find all files that match a certain pattern and tested:
for theProblem in $(find $DIRECTORY -regex '29_foo[0-9].bar'); do
and I'm returned:
No such file or directory
Further down the rabbit hole I found How to use regex in file find so I tried:
for theProblem in $(find $DIRECTORY -regextype posix-extended -regex '29_foo[0-9].bar'); do
and terminal tells me:
find: -regextype: unknown primary or operator
Why in Yosemite am I not able to target the files with -regex
? When I do man regex
I am returned the manual, my bash version is 3.2.57. So why does -name
work when -regex
will not?