While all answers are more or less the same, I don't find them readable with multiple name and Boolean operators in-between.
I think this may be more elegant solution:
$ find . -type f | grep -E "\.java$|\.cpp$|\.c$"
Let's break this up
find .
finds all files recursively in current path (change to some other path if you need)
-type f
narrows the search only to files (not too much of a speed gain, but still...)
| grep -E
I used this to get grep recognize or (|
) operator in Mac OS X which uses FreeBSD grep, GNU grep does not need that (check in your man
file).
"\.java$|\.cpp$|\.c$"
regular expression which includes files whose name ends with .java
, .cpp
, and .c
(add ones you need)
You can then pipe the resulting list for further processing, e.g.
$ find . -type f | grep -E "\.java$|\.cpp$|\.c$" | xargs sed -i '' $'/s/\r$//'
This example removes DOS/Windows CRLF
line ending for OS X/Linux LF
(this is also OS X sed
syntax, check for your version specifics).
find
command, since that is where you are searching for matching files.find -print0 | xargs -0
construct or even better and simpler:find . -name '*.cpp' -o -name '*.c' -o -name '*.h' -exec wc -l {} +
. This will avoid any file name issues (blank spaces, new lines and so on) and is (very) good custom.