Total Pageviews

Tuesday, April 9, 2013

Find or grep a string in a set of files

Find a string in a set of files:

$find . -name *.c | xargs grep my_func_name

        This command searches all the directories under the current directory for ".c" files.  In each of the file it searches for the string "my_func_name".  Lists out the matching files and string.

$find . -name *.[ch] | xargs grep my_func_name

        This command searches all the directories under the current directory for ".c" and "*.h" files.  In each of the file it searches for the string "my_func_name".  Lists out the matching files and string.

$find . -name *.[ch] | xargs grep my_func_name 2>/dev/null

        This command searches all the directories under the current directory for ".c" and "*.h" files.  In each of the file it searches for the string "my_func_name".  Lists out the matching files and string.
Also it does not print the error messages found during search on to the console.

$find . | grep Myfile | xargs grep my_func_name 2>/dev/null

        This command searches all the directories under the current directory for files containing "Myfile" in its name.  In each of the file found it searches for the string "my_func_name".  Lists out the matching files and string. Also it does not print the error messages found during search on to the console.