Unix find: multiple file types

UnixFind

Unix Problem Overview


I want to run find -name with multiple file types. Eg.

 find -name *.h,*.cpp

Is this possible?

Unix Solutions


Solution 1 - Unix

$ find . -name '*.h' -o -name '*.cpp'

To find this information in the man page, type man find and the search for operators by typing /OPERATORS and hit enter.

The . isn't strictly necessary with GNU find, but is necessary in Unix. The quotes are important in either case, and leaving them out will cause errors if files of those types appear in the current directory.

On some systems (such as Cygwin), parentheses are necessary to make the set of extensions inclusive:

$ find . \( -name '*.h' -o -name '*.cpp' \)

Solution 2 - Unix

Thats what I use

find . \( -name "*.h" -o -name "*.cpp" \) -print

Solution 3 - Unix

find . -name "*.h" -or -name "*.cpp"

works for me.

Solution 4 - Unix

You can also use the -regex utility:

find -E . -iregex ".*\.(js|jsx|html|htm)"

Remember that the regex looks at the full absolute path:

For an explanation of that regex with test cases check out: https://regex101.com/r/oY1vL2/1

-E(as a flag BEFORE the path) enables extended (modern) regular expressions.

This is for BSD find (Mac OSX 10.10.5)

Solution 5 - Unix

find . -name '*.h' -o -name '*.cc'`

works for searching files.

find . \( -name '*.h' -o -name '*.cc' \)`

works for executing commands on them

find . \( -name '*.h' -o -name '*.cc' \) -exec egrep "#include" {} \; -print | egrep "^\."

Solution 6 - Unix

That's what I use.I strongly recommend the "-regextype posix-extended" argument.

find . -type f -iname "*.log" -o -iname "*.gz" 
find . -type f \( -name "*.gz" -o -name "*.log" \)
find . -type f -regex '.*\(\.gz\|\.log\)'
find . -type f -regextype posix-extended -regex '.*.(log|gz)'

Solution 7 - Unix

find ./ -name *.csv -o \\-name *.txt|xargs grep -i new

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJackoView Question on Stackoverflow
Solution 1 - UnixEric WilsonView Answer on Stackoverflow
Solution 2 - UnixPierre LacaveView Answer on Stackoverflow
Solution 3 - UnixjhamView Answer on Stackoverflow
Solution 4 - UnixEvanView Answer on Stackoverflow
Solution 5 - Unixuser4795194View Answer on Stackoverflow
Solution 6 - UnixxoyabcView Answer on Stackoverflow
Solution 7 - Unixamritpal singhView Answer on Stackoverflow