简体   繁体   中英

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

How can we find specific type of files ie 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?

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/bash
shopt -s globstar
shopt -s nullglob
for file in **/*.{pdf,doc}
do
  echo "$file"
done
find . | grep "\.doc$"

这也将显示路径。

可以使用的其他一些方法:

echo *.{pdf,docx,jpeg}

stat -c %n * | grep 'pdf\\|docx\\|jpeg'

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

Similarly if you prefer using the wildcard character * (not quite like the regex suggestions) you can just use ls with both the -l flag to list one file per line (like grep) and the -R flag like you had. Then you can specify the files you want to search for with *.doc IE Either

ls -l -R *.doc

or if you want it to list the files on fewer lines.

ls -R *.doc

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM