Thursday, May 26, 2016

How to exclude some files in "grep"


        When we are searching for a string in a list of files using "grep" command, we may want to exclude some of the files from grep. We can achieve that with the help of the "--exclude" option in the grep command.

Example:

        For example, we are having text files name x, y, z and we want to search for "string" in these files except z. Then we can exclude the file z from the grep as given below.

        grep -nir "string" * --exclude z

        This command will search for the token "string" in all the files except file "z".

Excluding Multiple Files:

       To exclude multiple files from grepping, we can specify the command,

        grep -nir "string"  * --exclude file1 --exclude file2