Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am looking for files which contains a string for example "my text". But I don't know where the file locates. So I look for all the files do as follow (seems crazy):
Code:
grep -r "my text" *
But I can't get any result though there are indeed files which contain such string.
I am looking for files which contains a string for example "my text". But I don't know where the file locates. So I look for all the files do as follow (seems crazy):
Code:
grep -r "my text" *
But I can't get any result though there are indeed files which contain such string.
Is this command wrong?
Yes. Try this:
Code:
path="/path/of/interest"
search="my string"
find $path -type f | while read filepath
do
if grep -iqP "\b$search\b" "$filepath"
then
echo "String \"$search\" found in file \"$filepath\"."
fi
done
path="/path/of/interest"
search="my string"
find $path -type f | while read filepath
do
if grep -iqP "\b$search\b" "$filepath"
then
echo "String \"$search\" found in file \"$filepath\"."
fi
done
I find your solution is a little bit too complicated.
path="/path/of/interest"
search="my string"
find $path -type f | while read filepath
do
if grep -iqP "\b$search\b" "$filepath"
then
echo "String \"$search\" found in file \"$filepath\"."
fi
done
This will be quite slow for a large number of hits as you're launching a new instance of grep for every file you find; you can just put the grep into the find command using the + instead of \; termination, then grep will be given a large number of files instead of only a single file:
Code:
find /path/of/interest -type f -exec grep "my string" "{}" +
This will be quite slow for a large number of hits as you're launching a new instance of grep for every file you find; you can just put the grep into the find command using the + instead of \; termination, then grep will be given a large number of files instead of only a single file:
Code:
find /path/of/interest -type f -exec grep "my string" "{}" +
Just a thought.
Yes, you are right. It will take very very long to find out the files. I am not sure if there is a best way.
The problem by your solution is, one has to know the path.
Yes, you are right. It will take very very long to find out the files. I am not sure if there is a best way.
The "best way" does not exist. For anything. All you can di is find the approach which suits the problem you are trying to overcome with minimal negative side effects.
Quote:
Originally Posted by thomas2004ch
The problem by your solution is, one has to know the path.
Not necessarily. You can supply a search path of /, or refine the other search criteria further, using mtime/atime/ctime, size etc.
I am looking for files which contains a string for example "my text". But I don't know where the file locates. So I look for all the files do as follow (seems crazy):
Code:
grep -r "my text" *
But I can't get any result though there are indeed files which contain such string.
Obvious newbie question(or answer?)... Why couldn't you just use
ls / | grep -switch "string"
?
Because that would only pass a list of the directories immediately under / to grep which would then only output those that matched "string". Using find or a recursive option allows the full tree to be passed. For example, given the following directory structure:
Your code would only pass the subdirs as arguments to the grep command. Using either find or -R passes the subdirs and the subsubdirs - the commands travel through the entire tree.
Because that would only pass a list of the directories immediately under / to grep which would then only output those that matched "string". Using find or a recursive option allows the full tree to be passed. For example, given the following directory structure:
Your code would only pass the subdirs as arguments to the grep command. Using either find or -R passes the subdirs and the subsubdirs - the commands travel through the entire tree.
This is what the manpage has to say on the matter:
Quote:
Originally Posted by man grep
-R, -r, --recursive
Read all files under each directory, recursively; this is
equivalent to the -d recurse option.
However, my experience is this is not so fool-proof as using find to get a list of files, then passing that list to grep separately. See above for my suggestion.
Also, `ls` does not produce a full path of the file being examined, so while the -R option will cause recursion of `ls` itself, it will not produce helpful results, like a full path to the file wherein the string may have been found.
EDIT: And, as pwc101 hinted at above, ls can produce results that the next-in-pipe command has troubles with, producing weird errors.
Cheers
Sasha
Last edited by GrapefruiTgirl; 09-15-2009 at 11:09 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.