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

I like using grep but sometimes I just want to search for the names of files in a directory tree, but don't want grep to open the files and search inside them. Can grep do this?

share|improve this question

4 Answers 4

up vote 5 down vote accepted

There are many answers to this question but the most 'correct' traditional unix answer is to use find(1):

find / -type f -name foo\*

will find every file under the current directory with a name that starts with foo

share|improve this answer
    
Actually, that command will find everything under the root directory. To find everything under the current directory you should use . as the first argument. You can also use any file path as the first argument. –  Ladadadada Nov 16 '11 at 16:28

Try a regular expression, like

ls -R |grep ^a

This will list all files and subdirectories and the will filter on names starting with a.

This doesn't include a path though.

share|improve this answer

There are many ways to find files in linux. Best in my view are two. Find and Locate 'find' is much more powerful then locate. find command's has one limitation, which is, its speed at which it gives you results of your search. This issue is resolved by locate command. which stores a local cached tree of whole directory in it,(which is manually built by running UPDATEDB command) and when you want to search for a file, then simply use "locate filename", and you will get results instantly. when speed is issue, use locate, for anything else , use find command.

share|improve this answer

also you can use locate command

so first you should update locate Db by issuing updatedb command (note it could take some time for finish)

than run

locate foo*

You'll get full path to the file

share|improve this answer

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.