[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

Which file contains the specified string?

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

Phoebe

0
Phoebe
Asked:
Phoebe
  • 4
  • 3
  • 2
  • +2
1 Solution
 
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
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
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
 
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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now