1

I'm curious if anyone has a way to automatically generate a different set of --exclude-dir flags for grep for each different codebase that you work in.

Because in one project, I'll never want to search in the output/ dir, and in another, I'll never want to search in the checkpoints/ directory. Since there are often a few folders in a codebase that have non-source code, I always have to type those out manually (e.g. grep --exclude_dir {onedir, twodir, threedir} ...)

Ideally, for whatever codebase I'm in, I can just have a file in it that grep can look into to see what to automatically populate the --exclude-dir option with whenever I call grep from the command line. So that I don't have to type out a long list of directory names to exclude every time, which differ based on which project I'm currently working in.

If it could just read from an existing .gitignore that would work too.

1

The following script works for me

#!/bin/bash

if [ -r .grepignore ]; then
    while read -r line; do
        if [[ "$line" =~ '#' ]] || [[ "$line" =~ ^$ ]]; then
            continue
        fi
        EXCLUDE="$EXCLUDE --exclude-dir=\"$line\""
    done < .grepignore
fi

eval grep -r "$EXCLUDE" '"$@"'

The script checks whether a file .grepignore exists and is readable. If so, it constructs a string EXCLUDE containing --exclude-dir="$line", for any line $line in that file (without leading and trailing whitespace), provided it is not empty or contains a #. The script then passes that EXCLUDE string to grep and also adds arguments you gave to the script.

In order to execute the script, save the code in a file mygrep, make it executable with chmod +x /path/to/mygrep, and call it with /path/to/mygrep, followed by any options you would give to grep and of course by the search term.

1

Looks like the --exclude-from=FILE flag is what you're looking for. You'll have to create and populate manually each project-specific FILE with entries like :

onedir/*
twodir/*

For convenience, you can also create a shell alias so that running grep will automatically call the --exclude-from=FILE flag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.