Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

using bash shell, how can I search for a particular file in a folder (and its sub-folders)

share|improve this question

2 Answers 2

up vote 5 down vote accepted
find dirname -name "filename"

For example:

find /usr/bin -name "*sh"

would find all the files that end in "sh" in that directory.

Or:

find /usr/share/doc -iname "*readme*"

would find all files with "readme" in their names regardless of capitalization in that directory and its subdirectories.

share|improve this answer
    
thanks, what about paging the results? –  user2659 Dec 2 '09 at 22:11
1  
just add "| less" to the end of your find command –  3dinfluence Dec 2 '09 at 22:13
    
Are there any systems without less any more? If so, then | more will do an OK job. Or for really old systems: | pg - I had almost forgotten about that one. –  Dennis Williamson Dec 2 '09 at 22:25

There are two ways to do this.

If slocate is installed and the database is up to date you can use "locate pattern | grep /path/you/wish/tosearch"

This is faster than the find command if you're searching a large directory or directories.

The other more traditional method is using the find command as Dennis has already pointed out so I won't go into that one.

share|improve this answer
    
The locate database is not updated frequently enough to use it for this purpose, most of the time. –  Michael Graff Dec 2 '09 at 20:04
    
That all depends on your data. And how often updatedb runs. Works fine on most systems as long as the file you're looking for is more than a day old. In which case you'll need to use find. –  3dinfluence Dec 2 '09 at 20:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.