1

I need files ending with ".pdf" or ".png"; here's my attempt:

find /Users/robottinosino/Desktop/_PublishMe_ -type f -regex '.*[pdf|png]'

this incorrectly includes files ending with "Apdf", "Zpdf", etc. (missing literal dot before file extension)

I tried adjusting the pattern to:

find /Users/robottinosino/Desktop/_PublishMe_ -type f -regex '.*\.[pdf|png]'

but then no results are returned. Escaping the . with a backslash does not work. Why?

[0] $ uname -a

Darwin Robottinosino.local 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386

Thanks!

1

6 Answers 6

3

The problem isn't the dot. It's the brackets. Square brackets define a character class; i'm fairly sure what you're trying to do is group an alternation. For that, you need round brackets. You need to use extended regular expression for that, and so the command is:

find -E /Users/robottinosino/Desktop/_PublishMe_ -type f -regex '.*\.(pdf|png)'

That -E flag is a BSDism (OS X having a largely BSD-ish userland). On GNU find, you instead say -regextype posix-extended, as an expression rather than a flag (according to Dennis Williamson's comment, which is doubtless correct).

3
  • Curiously, the second one works like a charm but the 1st one doesn't? The one without the "-E"? What's wrong? Jun 24, 2012 at 15:02
  • I guess if I don't turn on modern REs with -E, the command fails because of this: Obsolete (``basic'') regular expressions differ in several respects. `|' is an ordinary character and there is no equivalent for its functionality. (ref: man re_format on OSX) Jun 24, 2012 at 15:07
  • Ah, sorry to have misled you on that. I tried a related version of that command locally, but without the |, so i didn't catch that. My mistake. I will remove that suggestion. Jun 24, 2012 at 15:54
5

You could just use something like \( -name '*.pdf' -or -name '*.png' \) instead unless you have another reason to match with a regex.

3
  • I need to match files with either extension in one go.. Jun 24, 2012 at 14:47
  • That's what the -or is for.
    – millimoose
    Jun 24, 2012 at 14:52
  • I did not know about -or, thanks for teaching me that. Jun 24, 2012 at 15:02
2

The pattern [pdf|png] matches any one of the characters within the square brackets (including the pipe).

Try this:

find /Users/robottinosino/Desktop/_PublishMe_ -type f -regex '.*\.\(pdf\|png\)'
6
  • Hi Dennis. Thanks for answering. This does not work... but if I prefix "-E" and then rephrase to "-regex '.*\.(pdf|png)' it does... why did you escape the pipe? Is this different behaviour due to the platform? Jun 24, 2012 at 14:59
  • @Robottinosino: The find (find (GNU findutils) 4.4.2) on my system does not have -E. It does have -regextype posix-egrep and -regextype posix-extended (also posix-awk) which work without escaping the parentheses and pipe. Jun 24, 2012 at 15:12
  • I still voted you up in gratitude! ;) [I did specify I was on OSX in the original question...] Jun 24, 2012 at 15:14
  • @Robottinosino: Sorry, I failed to notice the tag osx. Jun 24, 2012 at 15:15
  • If you're not familiar with OS X, the tag is the only hint that that's the platform, but if you are, the /Users/... absolutely screams Mac. Jun 24, 2012 at 15:53
0
find /Users/robottinosino/Desktop/_PublishMe_ -type f -regex '.*\.[pdf|png]'
0

You should be using -name *.pdf and not .*[pdf|png].
Your regex will match .Apdf, .Zpng etc.

You can try this:
find . -type f | egrep '.pdf$|.png$'

1
  • The OP already noticed the first mistake, the problem is in his second attempt.
    – millimoose
    Jun 24, 2012 at 14:55
0

This should suit:

find /Users/robottinosino/Desktop/_PublishMe_ -type f -regextype posix-egrep -regex '.*\.(pdf|png)'

If your version of find does not know about -regextype posix-egrep, then you can try this, instead:

find /Users/robottinosino/Desktop/_PublishMe_ -type f '.*\.\(pdf\|png\)'
1
  • find: -regextype: unknown option :( Jun 24, 2012 at 15:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .