Which file contains the specified string?

How can I find which file contains the specified string
in the giving directory?

Phoebe

PhoebeAsked:
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.

ozoCommented:
grep string directory/*
0
jonkeCommented:
Or if you do not know that it is in any particular directory, i.e., you think this file is in /opt, but it might be deep down in some sub-directory, this might be of use (All one command, though it isn't fitting on the one line):

find $DIR -local -type f -name '*' -exec fgrep -li $STRING {} \; >> temp_string_file;cat temp_string_file |grep -v fgrep;rm temp_string_file

Where $DIR= the directory to search from, and $STRING=The string of characters to search for- but there can be no spaces in this.

Be aware that it can take some time to run, and don't start it too high in the directory structure,  or it will take forever to run- remember depending on where you run it from, you maybe forcing it to look through thousands of files.

0
jonkeCommented:
I just used in to find the string 'onny' in all the files in /opt. It took around 10 minutes to run.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

jonkeCommented:
Also it is best to run the command as root
0
PhoebeAuthor Commented:
jonke


when I key in
 find . -local -type f -name '*' -exec fgrep -li 'ilx'   \; >> temp_string_file

I got  a file size=0;

I mean looking inyo current directory
and subdirectory , which file include
string 'ilx'


Could you give me an example.

thank you
0
chris_calabreseCommented:
First, unless you don't want to cross NFS mounts, there's no reason to put in the -local.  Similarly, the -name '*' will only serve to eliminate files starting with '.', so you probably to get rid of that too.

That leaves
  find . -type f -exec fgrep -il 'ilx' \; >> temp_string_file

If temp_string_file is empty, that means there were no matches.
0
jonkeCommented:
I find that doing a find across nfs mounts can often hang it.
0
chris_calabreseCommented:
The problem is that most find's implement -local in such a way that they'll still hang if an NFS mount is down because they have to traverse the mountpoint to find out whether it is local or not.  The only way not to hang in this situation is to implement a timeout on the test, and I don't know of any finds that actually do things that way (though I've patched a few such programs myself over the years to do this).
0
RobWMartinCommented:
Phoebe,

find . -type f -exec fgrep -il 'ilx' \{\} \; >> temp_string_file

you must include the \{\} or fgrep will try to read from standard input, not the file found by 'find'

Or maybe you are trying to find 'ilx' in the name of the file:

find . -name '*ilx*'


Hope this helps!

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
chris_calabreseCommented:
Hmm, this seems remarkably similar to other ideas in previous comments.  Phoebe, I think it would be most fair to reject this answer and allow jonke to submit an official answer, since he's the first one who came up with an appropriate find command.
0
PhoebeAuthor Commented:

The answer is working veryveryvery good. I would remember it
forever. \{\} is a must.

I understand

find . -name '*ilx*' -print

I did want to find which file contains 'ilx'.  

Thank you and thank you.

Phoebe
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
Unix OS

From novice to tech pro — start learning today.