How to list specific type of files in recursive directories in shell?
How to list specific type of files in recursive directories in shell?
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?
alok.doc.txt
6 Answers
6
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.
probably good to escape the dot, otherwise a file like
hellodoc would also match. ls -R | grep ".doc$"– fedorqui
May 18 '15 at 9:25
hellodoc
ls -R | grep ".doc$"
Is there a way to have this show you the full path of a file that
ls finds? Right now I just a list of all the files, but I don't know how to actually go find them now.– Russell
Sep 17 '15 at 15:31
ls
never, ever use
ls as input data in scripts– MestreLion
Sep 8 '17 at 4:09
ls
ls command output is mainly intended for reading by humans. For advanced querying for automated processing, you should use more powerful find command:
ls
find
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
It's */.pdf */.doc - you are not recursing into the the sub directories without it
– Petesh
Aug 20 '10 at 6:04
its actually double asterix
– ghostdog74
Aug 20 '10 at 6:11
You can do
ls **/*.pdf,doc or for file in **/*.pdf,doc– Dennis Williamson
Aug 20 '10 at 6:33
ls **/*.pdf,doc
for file in **/*.pdf,doc
yes of course. totally forgot about brace expansion
– ghostdog74
Aug 20 '10 at 6:50
Wow, I typed in three asterisks and it replaced it with one - something to know in the future.
– Petesh
Aug 20 '10 at 10:27
Some of the other methods that can be used:
echo *.pdf,docx,jpeg
echo *.pdf,docx,jpeg
stat -c %n * | grep '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
find . | grep ".doc$"
This will show the path as well.
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
I.E. Either
*
ls
-l
-R
*.doc
ls -l -R *.doc
or if you want it to list the files on fewer lines.
ls -R *.doc
Did you try running this? mkdir tmp; cd tmp; mkdir a; touch a/a.doc; ls -l -R *.doc
– Kumar Alok
Jul 14 '16 at 14:09
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
+1, Learning something new about linux everyday.
– John Odom
Feb 28 '14 at 15:39