Possible to use grep simply for finding files in a directory structure?

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?


Solution 1:

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

Solution 2:

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.

Solution 3:

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.

Solution 4:

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