1

I'm currently using this command to get a list of files :

find -E . -regex '.*\.(jpg|jpeg|png|gif|pdf)'

Using that command, I would get this list :

Now :

documentA.pdf
documentB.jpeg
001documentC.png
...

My aim is to filter this list, so I would only get files beginning with 3 integers and ending with the extensions previously set.

Goal :

001documentC.png

How can I tweak the command find -E . -regex so I would get such behaviour ? I have created a regex string : ([0-9][0-9][0-9]).*\.(jpg|jpeg|png|gif|pdf), tested on regexPal.com but I couldn't make it work with the find command.

Jean
  • 173
  • are you still putting the speechmarks around it? `'([0-9][0-9][0-9]).*.(jpg|jpeg|png|gif|pdf)' ? I can't see anything obviously wrong with this ... – djsmiley2kStaysInside Mar 13 '17 at 12:22
  • Looks like an uncommon behaviour of find. What's your find version? – iBug Mar 13 '17 at 12:28
  • What does the -E option mean? It doesn't work with my find. – simlev Mar 13 '17 at 13:42
  • According to man find the whole path is checked, so find . -regextype awk -regex '.*/[0-9][0-9][0-9].*\.(jpg|jpeg|png|gif|pdf)' works for me. – simlev Mar 13 '17 at 13:43
  • That did not work on my terminal. I'm using MacOs an zsh, that may be the reason why. Another example which should have worked did not, that's why I think zsh may be responsible. – Jean Mar 13 '17 at 18:23
  • From a quick look at osx's find man page, it seems you should take care to include the slash and use \d instead of [0-9]. With find -E . -regex '.*/\d\d\d.*\.(jpg|jpeg|png|gif|pdf)' do you get an error message or just no files found? – simlev Mar 13 '17 at 19:11
  • No files were returned. – Jean Mar 14 '17 at 18:49

1 Answers1

1

Found the perfect solution thanks to this stackoverflow question

find -E . -iregex '.*/*\.(jpg|png|eps|gif)'

Test :

$ touch documentA.pdf documentB.jpeg 001documentC.png 0documentD.eps 0000documentE.pdf
$ find -E . -iregex '.*/[0-9]{3}.*\.(jpg|png|eps|gif)'

./001documentC.png
Jean
  • 173
  • This is equivalent to the filter I posted earlier in my comment: '.*/[0-9][0-9][0-9].*\.(jpg|jpeg|png|gif|pdf)' where I pointed out the whole path is filtered (and therefore you should add a slash before the filename). You commented it didn't work probably because you copy/pasted the whole line instead of just the filter. Option -regextype awk is the (GNU find) equivalent of -E (in MacOS find). – simlev Mar 17 '17 at 08:47