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

Viewed 73438

How can we find specific type of files i.e. doc pdf files present in nested directories.

command I tried:

$ ls -R | grep .doc

but if there is a file name like alok.doc.txt the command will display that too which is obviously not what I want. What command should I use instead?

7 Answers
find . | grep "\.doc$"

This will show the path as well.

We had a similar question. We wanted a list - with paths - of all the config files in the etc directory. This worked:

find /etc -type f \( -iname "*.conf" \)

It gives a nice list of all the .conf file with their path. Output looks like:

/etc/conf/server.conf

But, we wanted to DO something with ALL those files, like grep those files to find a word, or setting, in all the files. So we use

find /etc -type f \( -iname "*.conf" \) -print0 | xargs -0 grep -Hi "ServerName"

to find via grep ALL the config files in /etc that contain a setting like "ServerName" Output looks like:

/etc/conf/server.conf: ServerName "default-118_11_170_172"

Hope you find it useful.

Sid

If you have files with extensions that don't match the file type, you could use the file utility.

find $PWD -type f -exec file -N \{\} \; | grep "PDF document" | awk -F: '{print $1}'

Instead of $PWD you can use the directory you want to start the search in. file prints even out he PDF version.

Related