LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 08-27-2008, 04:32 PM   #1
luusac
LQ Newbie
 
Registered: Aug 2008
Posts: 10

Rep: Reputation: 0
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
 
Old 08-27-2008, 04:40 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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.
 
Old 08-27-2008, 04:52 PM   #3
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285
I would do:

Code:
find . -iname "*hesketh*.doc"
 
Old 08-27-2008, 05:07 PM   #4
luusac
LQ Newbie
 
Registered: Aug 2008
Posts: 10

Original Poster
Rep: Reputation: 0
many thanks to you both, I will try these.
lu
 
Old 08-27-2008, 07:36 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by H_TeXMeX_H View Post
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.
 
Old 08-28-2008, 03:56 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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"
 
Old 08-28-2008, 04:14 AM   #7
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285Reputation: 1285
Quote:
Originally Posted by matthewg42 View Post
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"
 
Old 08-28-2008, 12:00 PM   #8
luusac
LQ Newbie
 
Registered: Aug 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by matthewg42 View Post
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
 
Old 08-28-2008, 12:07 PM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by luusac View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
alias not working as expected marozsas Linux - General 3 06-01-2006 01:46 PM
cannot execute the find command as expected ? kushalkoolwal Programming 5 01-25-2006 11:33 PM
Crontab not working as expected nro Linux - Newbie 7 08-29-2005 01:56 PM
fontconfig: Didn't find expected font family toffi22 Linux - Software 1 06-27-2005 01:04 PM
up2date not working as expected johnny13 Linux - Newbie 7 07-21-2003 02:15 PM


All times are GMT -5. The time now is 06:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration