I am trying to find all the files (ending with .c,.h and .java) that are in a folder or its subdirectories beginning with "new-". This is my current command: ls -R | egrep '.[.]c$|.[.]h$|.*[.]java$' What should I change?
Use find with
-o (or operand) and (escaped) parentheses:
find $dir \( -name 'new*.java' -o -name 'new*.c' \)
Technically the parentheses aren't required unless you add other filters.
You can also use a regexp:
find . -regextype egrep -regex '.*/new-.*\.(java|c)'
(the regexp should match the full path, hence the
.*/ at the start.
It's kind of ugly, but this would output each file extension you wanted in your current directory and it's sub-directories
find . -type f -name '*.c' && find . -type f -name '*.h' && find . -type f -name '*.java'
If you have to use ls then this might be what you are looking for:
ls | grep -r '*.c\|*.h\|*.java' | grep new-*