Finding files in Linux that contain a text string

This seems like such a simple task, but I haven't found a simple, fast way to do this in Linux.  How can I find the file name in a particular directory that contains a particular word (or text string) when I don't know the file name?  (I usually do know the file extension.)

For example, recently I needed to find the file that contained the word "indexfile".  I knew the directory it was in, and I knew that the file extension was "ctl".  This command was very fast, and it confirmed that the file was there somewhere, but it didn't give me the file name:

$ cat *.ctl | grep indexfile

That quickly returned this:

indexfile=bartector_indexes.sql

What Linux command can I use to search the *.ctl files in my current directory and give me the name of the file that contains this search string?  If that command can also return the line of text and/or the line number in the file that would also be helpful.
LVL 36
Mark GeerlingsDatabase AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MazdajaiCommented:
grep -R indexfile .

Open in new window

0
dipopoCommented:
updatedb

locate .ctl
0
Mark GeerlingsDatabase AdministratorAuthor Commented:
To Mazdajai:

That seems to take a *VERY* long time (and it hasn't returned anything yet).  Isn't there a way that I can help it narrow the search by providing the file extension and/or a portion of the file name if I know that?
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Mark GeerlingsDatabase AdministratorAuthor Commented:
To dipopo:

Yes, if all of my files were on local disk or on a SAN, that would work.  But, we now have a NAS, not a SAN.  And, as far as i know, the "updatedb" command excludes nfs-mounted remote volumes, which is where most of my files are.
0
dipopoCommented:
updatedb should work for anything under /mnt , it will not work for /media though. Since this is a NAS, I'm guessing these disks were mounted!?

Else edit the config of updatedb and remove /media from the PRUNEPATHS or change the location of your NAS "volumes" from /media to /mnt using fstab
0
Seth SimmonsSr. Systems AdministratorCommented:
remove nfs/nfs4 from the prunefs line in your /etc/updatedb.conf
updatedb will go through your nfs mounts and process that also
if you want to exclude certain mounted paths, add those in the prunepaths line
0
Mark GeerlingsDatabase AdministratorAuthor Commented:
We don't have "nfs" in our /etc/updatedb.conf files, but with OracleEnterpriseLinux5, it seems that remote nfs volumes are not included.

Our remote directories are not mounted under either /mnt or /media.  We have separate, root-level mount points for these with names like: /u01, /u02, etc.

Here is what our /etc/updatedb.conf files look like:  (I think these are defaults.)
PRUNEFS = "auto afs gfs gfs2 iso9660 sfs udf"
PRUNEPATHS = "/afs /media /net /sfs /tmp /udev /var/spool/cups /var/spool/squid /var/tmp"
0
MazdajaiCommented:
Unfortunately recurvise search does take awhile. This is by design. Have you try limiting the current path?
0
Mark GeerlingsDatabase AdministratorAuthor Commented:
Yes, i said that earlier.  I want to limit the search to my current working directory (which in this case has just 80, relatively-small files and no sub-directories) *AND* I want to limit it to a particular file extension (*.ctl in this case) and sometimes to a portion of the file name.

I certainly do *NOT* want a system-wide recursive search through all directories!

I can very quickly (less than .1 second) do this:
time cat *.ctl | grep indexfile

That quickly scans my current directory of 80 small files, then reads each of the 14 files that have a file extension of "ctl", and confirms that one of them contains the text string I'm searching for.  But this "cat ... grep" command does not indicate which file it found this string in.  So, this command is very fast, but not so helpful.

"grep -R" may be helpful (if it would ever finish) but I don't like waiting half a day for it!
0
MazdajaiCommented:
You can add wildcard or limit the extension to the search -

grep -RH indexfile *\.ctl

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark GeerlingsDatabase AdministratorAuthor Commented:
Thank you, Mazdajai.  That seems to work as I expect, and it works for me either with or without the "\" character included after *.  

I'll accept your response after you tell me: What do you expect the "\" character there to do?

I did a bit more testing, I can also add an "n" option to see which line in the file this string occurs on.  I can remove the "R", since in most cases when I use this, I know that I don't need to search multiple levels of a directory tree. I can also remove the "H" option, and it still returns the file name.  This seems to contradict what I see in "man grep" where it indicates:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    -H, --with-filename
              Print the filename for each match.

    -h, --no-filename
              Suppress  the  prefixing  of  filenames  on output when multiple
              files are searched.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

To me, this means that if I don't include the "H" option, I should not expect to see the file name returned.  Is there a default list of "grep" options somewhere?  Or, is this something like the (frustrating to me at least) "ls" command that is recursive by default even when I don't add "ls -R".  There too, "man ls" leads me to believe the the "ls" command will not be recursive unless I add the "-R" option, yet "ls" always seems to be recursive without the "-R" option.
0
MazdajaiCommented:
What do you expect the "\" character there to do?

Sometimes you have to escape the special character in bash or bash will eat it.

It looks like you don't need -H when you have -R. By default grep does not display file name for a successful match. For example -
grep indexfile xxx.ctl
grep -H indexfile xxx.ctl

Open in new window

0
Mark GeerlingsDatabase AdministratorAuthor Commented:
THank you for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.