tags:

views:

132

answers:

4

I want to open many pdf -files which contain the word exam. My Mac's terminal uses Bash.

The word exam is randomly in the name: sometimes at the beginner, sometimes at the midlle and sometimes at the end of the name.

How can you open files which contain the word "Exam" in terminal?

+1  A: 

Something like

acroread *Exam*.pdf

should work. This matches any string that has "Exam" in it, and ends with ".pdf". This also assumes that you have a command called "acroread" that knows how to read PDF:s, that may or may not be true for Mac OS X.

unwind
On Mac OS X, you can open any file with the 'open' command. For PDF files it will launch Preview.app by default.
mouviciel
+6  A: 
find . -name "*exam*" -exec <name of pdf reader executable> {} \;
Visage
you might want `-iname` rather than `-name`, just in case the case varies.
vezult
+1  A: 

I would use:

ls *Exam* | xargs open
diciu
@diciu: Thank you! Your command works beautifully.
Masi
I wouldn't use ls like that. Use find instead.
Jeremy Cantrell
1. Don't parse ls, ever. 2. Don't use xargs unless you are 200% certain your filenames are really simplistic (or with -0 and NULL byte separated data). For short: Don't use ls or xargs. Learn find(1) or open *Exam*
lhunath
+1  A: 
  • Don't parse ls. It's output is not reliable and it's only made to look at (for human parsing). See http://mywiki.wooledge.org/ParsingLs.
  • Don't use xargs. It mangles your data trying to be smarter. Got spaces or quotes in your filenames? You can be sure that it'll explode.

To make xargs behave, you'd have to go to great lengths:

printf '%s\0' *Exam* | xargs -0 open

Yes, that's rather convoluted. Read on.

The find solution, while accurate, is recursive (might be what you want), but also a bit much to type in a prompt.

find . -name '*Exam*' -exec open {} +

You can make all that a lot easier by remembering that open on Mac OS X takes multiple arguments just fine (which is exactly what xargs does), so this is just fine for opening all documents in the current directory that contain the word Exam:

open *Exam*
lhunath