Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 676
Kumar Alok Avatar asked Aug 20 '10 05:08

Kumar Alok


People also ask

How do I list all files in a directory recursively?

Linux recursive directory listing using ls -R command. The -R option passed to the ls command to list subdirectories recursively.

How do I list files in a directory and subdirectories?

The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.


1 Answers

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.

like image 72
Benoit Courtine Avatar answered Sep 28 '22 03:09

Benoit Courtine