LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 07-22-2009, 09:32 AM   #1
matt007
LQ Newbie
 
Registered: Jun 2008
Posts: 14

Rep: Reputation: 0
searching within a file using bash


i get the desired result with

find . -name "*" | xargs grep -i "*searchPattern*"

---but not with

find . -name "*" | grep -i "*searchPattern*"

why this difference?

Thanks.
 
Old 07-22-2009, 09:57 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Assuming I'm not mistaken, in the first pattern, find sends a list of filenames to xargs, which then runs the grep command on the files listed. In the second one, find sends the list of filenames straight into grep as a text string. You're not searching inside the files themselves, only through the list.
 
Old 07-22-2009, 06:09 PM   #3
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,052

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
David is correct, but the correct use is:
Code:
find . -name "*" -exec grep -H searchpattern {} \;
jlinkels
 
Old 07-23-2009, 07:52 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Actually, neither one is really "wrong". The "find | xargs" version is generally more efficient though, because "-exec" executes the command separately for each file, while xargs batches the jobs up into a single process.

On the other hand, xargs often has trouble handling filenames with spaces in them, so -exec may be better in that situation.

On the gripping hand, I discovered just yesterday that modern versions of find can use -exec in an xargs-like fashion. You just have to change the final semi-colon to a plus-sign.
Code:
find . -name "*" -exec grep -H searchpattern {} \+
 
Old 07-23-2009, 08:36 AM   #5
matt007
LQ Newbie
 
Registered: Jun 2008
Posts: 14

Original Poster
Rep: Reputation: 0
thanks for the info.

But i was thinking how could one really tell that with

find . -name "*" | grep -i "*searchPattern*"

we are just passing in the file names as a text string to grep...is that a property of the find command that in this case returns a "text string", is there any way to run the command so as to be able to tell that....

thanks much.
 
Old 07-23-2009, 09:30 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Quote:
Originally Posted by matt007 View Post
thanks for the info.

But i was thinking how could one really tell that with

find . -name "*" | grep -i "*searchPattern*"

we are just passing in the file names as a text string to grep...is that a property of the find command that in this case returns a "text string", is there any way to run the command so as to be able to tell that....

thanks much.
The find command returns a multiline string which represent the names of the files that match the search criteria:
Code:
$ find . -type f
./testfile_0
./test2
./test3
./testfile_1
./test1
./test4
if you pipe this text to the grep command, you will search a pattern inside this text as you see it on the terminal. It is the same as:
Code:
echo "./testfile_0 ./test2 ./test3 ./testfile_1 ./test1 ./test4" | grep -i "*searchPattern*"
On the contrary if you pipe this text to xargs, it uses the text (that is the list of files in this case) as argument to the specified command (grep). It equals to execute:
Code:
grep -i "*searchPattern*" ./testfile_0 ./test2 ./test3 ./testfile_1 ./test1 ./test4
 
Old 07-23-2009, 09:35 AM   #7
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,052

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
Quote:
Originally Posted by David the H. View Post
Actually, neither one is really "wrong". The "find | xargs" version is generally more efficient though, because "-exec" executes the command separately for each file, while xargs batches the jobs up into a single process.
I didn't say it was wrong. But showing what the "correct" use of find -exec is, might be pedantic. I could have used a better wording like "usual", or "proposed".

Quote:
Originally Posted by David the H. View Post
Code:
find . -name "*" -exec grep -H searchpattern {} \+
It's Linux, so it is powerful, cryptic, regularly updated and hard to find. But a good suggestion.

Quote:
Originally Posted by matt007
But i was thinking how could one really tell that with
find . -name "*" | grep -i "*searchPattern*"
we are just passing in the file names as a text string to grep...
Yes, that is what the output is of find. When piping it into xargs it is processed by xargs in such a way that the file names in that string are processed. That is how xargs behaves, but it doesn't change anything to find

jlinkels
 
  


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
awk searching a string from a file within another file changcheh Linux - Software 7 12-29-2006 09:18 AM
bash searching directries and find the files bekirsagir Programming 1 08-19-2005 04:37 AM
Searching hard traffic bash programming forum satimis General 5 10-24-2004 12:25 PM
setting bash to look in current directory before searching the path muhkuhmasta Linux - Newbie 4 09-21-2004 02:08 AM
bash script for database searching using crontab saurya_s Linux - Software 5 01-22-2004 08:53 AM


All times are GMT -5. The time now is 04:53 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration