Linux - SoftwareThis 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
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.
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.
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.
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....
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:
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:
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
Quote:
Originally Posted by David the H.
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".
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.