How to list specific type of files in recursive directories in shell? How to list specific type of files in recursive directories in shell? unix unix

How to list specific type of files in recursive directories in shell?


If you are more confortable with "ls" and "grep", you can do what you want using a regular expression in the grep command (the ending '$' character indicates that .doc must be at the end of the line. That will exclude "file.doc.txt"):

ls -R |grep "\.doc$"

More information about using grep with regular expressions in the man.


ls command output is mainly intended for reading by humans. For advanced querying for automated processing, you should use more powerful find command:

find /path -type f \( -iname "*.doc" -o -iname "*.pdf" \) 

As if you have bash 4.0++

#!/bin/bashshopt -s globstarshopt -s nullglobfor file in **/*.{pdf,doc}do  echo "$file"done


find . | grep "\.doc$"

This will show the path as well.