Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
08-27-2008, 03:32 PM
|
#1
|
LQ Newbie
Registered: Aug 2008
Posts: 10
Rep:
|
find not working as expected
Hi,
I am trying to find (on a NAS) the string "Hesketh" anywhere in under root (recursive) but only in .doc or .Doc files.
I found this snippet on google:
find . -name *.doc | grep -lir "Hesketh" *
it has been chugging away for hours and so far has come up with:
EMAIL/LU/FOLD391O.PMM
EMAIL/LU/FOLO77O5.PMM
These are email folders, but the file extension is .PMM and not .doc
What is going on here - is it the fault of the trailing * ? But why, because the input of grep should be the output of find right? And find is only finding files of .doc type, right ?
Can anybody help?
thanks
lu
|
|
|
08-27-2008, 03:40 PM
|
#2
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
Find produces a list of files. If you pipe that list into grep, it will identify file names which match the pattern passed to grep as an argument.
If you want to search inside the files, use xargs to pass the listed file names to grep as extra arguments. This way grep will search inside these files.
If you use the -print0 option to find and the -0 option to xargs, file names will be delimited with the ASCII NUL character, avoiding problems with file names containing whitespace.
i.e.
Code:
find . -name '*.doc' -type f -print0 | xargs -0 grep -li "Hesketh"
The "-type f" tells find to only locate files (not directories). I also quoted the pattern '*.doc' to prevent it being pre-expanded by the shell in the case where there are one or more files matching that glob pattern in the present working directory.
|
|
|
08-27-2008, 03:52 PM
|
#3
|
LQ Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
|
I would do:
Code:
find . -iname "*hesketh*.doc"
|
|
|
08-27-2008, 04:07 PM
|
#4
|
LQ Newbie
Registered: Aug 2008
Posts: 10
Original Poster
Rep:
|
many thanks to you both, I will try these.
lu
|
|
|
08-27-2008, 06:36 PM
|
#5
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
Quote:
Originally Posted by H_TeXMeX_H
I would do:
Code:
find . -iname "*hesketh*.doc"
|
But that just searches the file names, not the contents. Luusac please clarify which you want - files with "hesketh" in the filename, or files with the string "hesketh" contained within the file.
|
|
|
08-28-2008, 02:56 AM
|
#6
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Code:
find . -name *.doc | grep -lir "Hesketh" *
Moreover, you obtained PPM files because you added an extra * as argument to the grep command, so that it did not parsed the piped output but all the items in the current directory.
I would add a little tip to the suggestion by matthewg42. If you want to look for either .doc or .Doc files you can use -iname to do a case insensitive search:
Code:
find . -iname '*.doc' -type f -print0 | xargs -0 grep -li "Hesketh"
|
|
|
08-28-2008, 03:14 AM
|
#7
|
LQ Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
|
Quote:
Originally Posted by matthewg42
But that just searches the file names, not the contents. Luusac please clarify which you want - files with "hesketh" in the filename, or files with the string "hesketh" contained within the file.
|
Oh, I see, if he wants that then I agree with colucix on this solution:
Code:
find . -iname '*.doc' -type f -print0 | xargs -0 grep -li "Hesketh"
|
|
|
08-28-2008, 11:00 AM
|
#8
|
LQ Newbie
Registered: Aug 2008
Posts: 10
Original Poster
Rep:
|
Quote:
Originally Posted by matthewg42
please clarify which you want - files with "hesketh" in the filename, or files with the string "hesketh" contained within the file.
|
files with extension matching .doc (case insensitive) which contain the string "Hesketh" (which can also be case insensitive) within the file (not filename)
lu
|
|
|
08-28-2008, 11:07 AM
|
#9
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by luusac
files with extension matching .doc (case insensitive) which contain the string "Hesketh" (which can also be case insensitive) within the file (not filename)
lu
|
So you should already have got the solution.
|
|
|
All times are GMT -5. The time now is 02:12 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|