How to find multiple file types using linux’s "find"

I’ve always found *nix’s “find” and “grep” rather hard to use. Not only are there different flavors of regular expressions to use, but mainly different syntax. For find, the directory you’re searching for comes first. For grep, it comes last. To find the negation of something, you’d use “-not” and for grep it’s “-v”. pain.

Anyway, been trying to learn my tools better, and I found out how to grep and replace expressions across multiple files through emacs. Since rails uses all sorts of file extensions, naturally, I wanted to grep for find different files types. I had thought the -name options took regexs (it doesn’t), so I had tried it in regex (no go)…only to find that it’s something like this:

find . -name "*.rb" -o -name "*.rhtml"

the -o is the equivalent of a boolean “or”. small tip…


8 thoughts on “How to find multiple file types using linux’s "find"

  1. thanks for that. it helped me for the same reason. regex syntaxes give me a headache as they differ between apps.That’s the beauty of standards – there are so many of them 😛

  2. You can always use the -regextype to make regex play nice. The problem with using -o is that if you want to pass all of your results to a program as a list with -exec you can’t because there needs to be a seperate -exec before each -o. For instance I wanted to search my music directory or videos and pass them all to mplayer to be played in a random order but I had multiple file types in multiple locations, this is what I came up with: find ./ -regextype posix-awk -regex “(.*.mpg|.*.avi|.*.wmv|.*.mpeg)” -exec mplayer -shuffle -loop 0 {} +

  3. After spending 30 minutes with “man find” I got frustrated and used search engine and after 5 minutes found this site and got my problem solved. Thank you!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s