Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 01-11-2015, 11:27 AM   #1
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>"


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, 01:10 PM   #2
Senior Member
Registered: Aug 2009
Distribution: CentOS
Posts: 3,344

Rep: Reputation: 1450Reputation: 1450Reputation: 1450Reputation: 1450Reputation: 1450Reputation: 1450Reputation: 1450Reputation: 1450Reputation: 1450Reputation: 1450
Just redirect the final output to a file:
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:
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:
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.)


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

All times are GMT -5. The time now is 01:15 AM.

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