You Are here: Home News Ubuntu / Linux / Unix Find with grep and xargs

20 -May -2012
Register

REGISTER

*
*
*
*
*
Fields marked with an asterisk (*) are required.
Find with grep and xargs PDF Print E-mail
News - Ubuntu / Linux / Unix
Written by Mark Veenstra   
Tuesday, 19 April 2011 19:05

If you want to search for files on your system and you want to search through the contents of these files to get a list matching (or not matching) the contents criteria, you will be using the find command. Ofcourse the find command has an '-exec' action. But is this efficient? To my knowledge and testing this is not efficient if you search through a lot of files. This can be done more efficient with adding xargs. For example:

You can do a find with a grep as follows:

[root@server ~]# find searchdir/ -type f -exec grep -il searchcriteria {} \;

This means that for every file that is found by the command find, grep will be executed. So if you search through 50.000 files, grep will be 50.000 times started which will consume a lot of time. With the time command added to the previous command the time results are when searching through 4995 files:

real    0m15.241s
user    0m7.197s
sys     0m7.875s

When we change the find command and add a pipe with xargs to it, the total time is less then the previous command, but the results are the same. This is because xargs will get the results of find from standard input and it will convert it into arguments for the grep command. Depending of the results of the find command, grep is executed as much as needed to complete the job. Arguments are limited to a maximum, so depending on this maximum and the results of find, grep is maybe only executed 10 times. Here is the find command with grep and xargs:

[root@server ~]# find searchdir/ -type f -print0 | xargs -0 grep -il searchcriteria

The time results of this command through the same 4995 files is:

real    0m3.453s
user    0m3.304s
sys     0m0.168s

As you can see this is pretty much faster than the first way and I only searched through 4995 files imagine what the time results will be when searching through 50.000 files! I also found that the exit codes of both commands are different. If you use the first find (with -exec) the result code will be normal (0). But in the last find the exit code is 127. Which sounds a bit strange, but I think this exit code comes from grep or xargs when the search criteria is not found in a file that has been grepped.



Share this....
Last Updated on Tuesday, 19 April 2011 19:39
 

Add comment

Please act and post as a normal adult!


Security code
Refresh