Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a bunch of directories. Inside these directories is a cover letter and a zip archive of work. I wanted to run a quick and easy query to open up all the cover letters so I can decide if it's worth looking at the work folder.

find . | grep cover\.pdf | xargs open    #OS X has open linked to "open" the file

This doesn't work. I've also muddied around with various print0, xargs -0 variations with little success. What's the proper way to do this?

share|improve this question
3  
"this doesn't work" translates to? nothing happening? an error message? your computer catching fire? –  hop Aug 24 '10 at 17:59
    
removed the "unix" tag, isn't everything unix already? –  phunehehe Aug 24 '10 at 18:34
    
@phu Well, there are 24 other posts tagged [unix]. It might end up getting blacklisted if it's so abused as to become useless, but we probably won't discuss that for a little while –  Michael Mrozek Aug 24 '10 at 21:54
    
@hop: It doesn't perform as expected, nothing gets opened. –  Josh K Aug 25 '10 at 1:00
    
Can you give us an ls -l of one of the directories that has the files you are looking for in please? I'm curious to find out why this isn't working. –  gabe. Aug 25 '10 at 14:04
add comment

3 Answers 3

First, always check what versions of find/xargs you actually use on your Mac-OSX box. Perhaps you expect BSD/GNU behavior/options and use GNU/BSD versions.

Don't know if open supports multiple arguments. If not call xargs like this:

xargs -n 1 open

(which means that xargs executes for every argument an extra open process)

And keep in mind that find | xargs without -print0/-0 is expected to fail if your filenames/paths contain spaces.

Aha, if you use something like

find -print0 | grep something | xargs -0 foo

then grep is confused by all the \0 characters.

GNU grep (at least) knows the -z option. Thus, which should work is:

find -print0 | grep -z something | xargs -0 foo

Check what grep version you use via

grep --version

Or check the man page of your grep for \0 (zero character) support.

share|improve this answer
    
This works better, but appending -print0 gives me an output of binary file matches in grep. –  Josh K Aug 25 '10 at 17:37
add comment

This should work:

find . -name cover\.pdf -exec open '{}' \;

You can test like this:

find . -name cover\.pdf -exec ls -l '{}' \;

Not sure what the man page looks like on OSX, but there are various examples in the find man page, if you search for EXAMPLES. I often find myself refreshing my brain by checking those.

share|improve this answer
    
I don't have an OSX box handy to test on though, so... it may not! –  gabe. Aug 24 '10 at 17:06
    
@gabe: This doesn't seem to work. –  Josh K Aug 24 '10 at 17:32
    
Tried it on a mac, and seems like the general case works... if you do the ls -l version above, what output do you get? –  gabe. Aug 24 '10 at 17:44
    
@gabe's suggestion totally works on OS X 10.6. I suspect you typed it wrong or something. –  Sandy Aug 24 '10 at 18:09
1  
use '{}' + not '{}' \; the + has better optimization... –  xenoterracide Aug 24 '10 at 19:14
show 4 more comments

If your xargs on your system supports -n option you can try following.

find . | grep cover\.pdf | xargs -n 1 open

share|improve this answer
add comment

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.