searching file names with grep

PaulS_III used Ask the Experts™
Hi all,
Please be patient, I am so very new to the UNIX world. I would like to know how I can use grep to search just file names? I know from the man pages and some other research I have done that grep will search files for the specified pattern, but I want to use it in conjunction with the ls command to see only those files that contain a certain word, or string of characters.

I appreciate your responses.

Regards all

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Grep searches files or its standard input. By placing a list of files on its standard input, you can get grep to search through the list.

For example:

find . -type f -print | grep 'your pattern here'

However, you should also know that find can do this directly (though it uses shell expression syntax rather than grep syntax):

find . -type f -name '*.fubar' -print
To use in conjunction with ls command as you asked:

   ls |grep string

   where "string" is what you want the files to contain.


if you wanto to  grep a string inside the files:

  grep string `ls`
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Your question is not clear. You may be trying to do one of several things.
1. You want to find a file whose name contains a string
2. You want to find a file whose contents contains a string

there are several things you need to clear for yourself as well:
are you looking for the file(s) only in the current directory? or in a directories tree?
What is the result you want to get: the name of the file? the directory its in? the line number that has the string?

To find files based on their name, especially if you look for them in a directory tree, find(1) is the command you need i.e.: find / -name \*X11\* -print
will print a list of all files whose name contain the string X11

To find files in the current directory whose contents has the string you look for, grep(1) is the tool as in:
grep -n X11 *
which will output the names of files in the current directory that contain the string X11

You can also combine find and grep and look for all files in a tree that contain a pattern as in:
find . -exec -s grep X11 () \; -print
which will search from the current directory downward, grep in each file for the string X11, and if the file contains X11, it will print its relative path name (starting from .)

I believe this answers you question, and don't forget, RTFM.


Nothing more to add here as DBD8 has explained it very well. The only correction which I would like to make is as follows:

To search a string in a file in different folders requires the combination of find and grep.

If you want to find out whether string 'hello' is appearing in how many files in your system then type the following:
find / -exec grep -il hello {} \;

If you want to restrict the search then specify the path accordingly after find (i.e. find /usr ...., find /tmp ... etc).

Hope this works for you.


Thank you all for your comments. DBD8 I do apologize for not making the task more clear. I am looking for files in my current working directory and not the whole tree. I also need to do a long listing of those file which have a particular character string anywhere in the file name, and the list needs to be sorted by most recent date/time.

To this end, I found that ls -lst will give me the listing I need in the order I need it, but I couldn't figure out how to get just the file names.

So with everyone's help I have got the following command structure of:

ls -lst | grep <character string>

This seems to provide me with the information I require.

Again, thanks for everyone's input.

I will leave this open for a bit longer. If any would like to add to what I have here I would like to see your thoughts.

ls -lstd *<character string>* is the simplest way.

ls -lstR |grep <character string> does a similar thing to combining find & grep, i.e. listing files in subdirectories.

If <character string> is complex, e.g. you want to find "profile" and ".profile", you can get into `grep` & regular expressions.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial