LinuxQuestions.org
Visit Jeremy's Blog.
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 01-11-2015, 12:27 PM   #1
sunbelt57
LQ Newbie
 
Registered: Aug 2013
Location: Wyoming
Posts: 19

Rep: Reputation: Disabled
using xargs and grep in find command


I've been using this a lot:

find <directory to start search at> -name "<files to search in>" -type f | xargs grep "<string to search for>"

e.g.

find /usr/include -name "*.h" -type f | xargs grep "#define UINT"

now what if I wanted to output the results to a file?
 
Old 01-11-2015, 02:10 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Just redirect the final output to a file:
Code:
find /usr/include -name "*.h" -type f | xargs grep "#define UINT" >/some/file
Your commands as written will run into a problem if files or directories with embedded spaces in their names are encountered. (You should never see that under /usr/include.) xargs will treat each part of that name as a separate argument. There are two ways to avoid that. You could tell both find and xargs to use a NUL character as a separator:
Code:
find /usr/include -name "*.h" -type f -print0 | xargs -0 grep "#define UINT" >/some/file
Or, you could avoid use of xargs altogether and let find invoke grep directly:
Code:
find /usr/include -name "*.h" -type f -exec grep "#define UINT" {} +
That final "+" tells find that grep will accept multiple file name arguments. Like xargs, find will put as many names as possible into each invocation of grep.

You might also want to include the "-H" option in the grep command. That will ensure that grep includes the file name in the output even in the somewhat unlikely case that it gets invoked with just a single filename argument. (A set of names that was just a tiny bit too long to fit in one command line would cause that.)
 
  


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
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 06:55 PM
clarification on find + xargs rm command dtra Linux - General 2 05-17-2006 06:56 AM
Help me to understand this command: find...| grep -e ... | xargs rm -f ROBERT483599 Red Hat 2 02-14-2006 09:21 AM
du on specific files from find command (use xargs?) fireman949 Linux - Software 2 07-10-2005 02:42 AM
PHP: what's wrong with this line (shell_exec, find, xargs, grep)? J_Szucs Programming 3 11-19-2003 08:44 PM


All times are GMT -5. The time now is 02:49 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