4

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?

5
  • 3
    "this doesn't work" translates to? nothing happening? an error message? your computer catching fire?
    – user601
    Aug 24, 2010 at 17:59
  • removed the "unix" tag, isn't everything unix already?
    – phunehehe
    Aug 24, 2010 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 Aug 24, 2010 at 21:54
  • @hop: It doesn't perform as expected, nothing gets opened.
    – Josh K
    Aug 25, 2010 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, 2010 at 14:04

3 Answers 3

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.

1
  • This works better, but appending -print0 gives me an output of binary file matches in grep.
    – Josh K
    Aug 25, 2010 at 17:37
2

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.

10
  • I don't have an OSX box handy to test on though, so... it may not!
    – gabe.
    Aug 24, 2010 at 17:06
  • @gabe: This doesn't seem to work.
    – Josh K
    Aug 24, 2010 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, 2010 at 17:44
  • @gabe's suggestion totally works on OS X 10.6. I suspect you typed it wrong or something.
    – Sandy
    Aug 24, 2010 at 18:09
  • 1
    use '{}' + not '{}' \; the + has better optimization... Aug 24, 2010 at 19:14
1

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

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

You must log in to answer this question.

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