Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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!

share|improve this question

migrated from stackoverflow.com Jun 25 '12 at 8:39

This question came from our site for professional and enthusiast programmers.

    
Uh. Sorry, man. :( –  Robottinosino Jun 24 '12 at 15:10

6 Answers 6

up vote 3 down vote accepted

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).

share|improve this answer
    
Curiously, the second one works like a charm but the 1st one doesn't? The one without the "-E"? What's wrong? –  Robottinosino Jun 24 '12 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) –  Robottinosino Jun 24 '12 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. –  Tom Anderson Jun 24 '12 at 15:54

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

share|improve this answer
    
I need to match files with either extension in one go.. –  Robottinosino Jun 24 '12 at 14:47
    
That's what the -or is for. –  millimoose Jun 24 '12 at 14:52
    
I did not know about -or, thanks for teaching me that. –  Robottinosino Jun 24 '12 at 15:02

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\)'
share|improve this answer
    
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? –  Robottinosino Jun 24 '12 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. –  Dennis Williamson Jun 24 '12 at 15:12
    
I still voted you up in gratitude! ;) [I did specify I was on OSX in the original question...] –  Robottinosino Jun 24 '12 at 15:14
    
@Robottinosino: Sorry, I failed to notice the tag osx. –  Dennis Williamson Jun 24 '12 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. –  Tom Anderson Jun 24 '12 at 15:53
find /Users/robottinosino/Desktop/_PublishMe_ -type f -regex '.*\.[pdf|png]'
share|improve this answer

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$'

share|improve this answer
    
The OP already noticed the first mistake, the problem is in his second attempt. –  millimoose Jun 24 '12 at 14:55

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\)'
share|improve this answer
    
find: -regextype: unknown option :( –  Robottinosino Jun 24 '12 at 15:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.