4

I often have to use the --exclude-dir to exclude various folders such as .git from the search path.

grep -r --exclude-dir=.git --exclude-dir=another_path XXX 

Then, the command line becomes quite lengthy every time.

Is there a way to make the --exclude-dir options the default, so that grep -r XXX is equivalent to above?

(This is with Ubuntu 24.04 LTS).

2
  • 3
    This question is similar to: Is there a 'rc' configuration file for grep/egrep? (~/.egreprc?). If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Jul 10 at 10:10
  • 3
    ripgrep (github.com/BurntSushi/ripgrep) may suite your situation better. It ignores various files/folders by default and is significantly faster than grep. It's very common and even VS Code nowadays calls ripgrep under the hood when you do a search due to its speed Commented Jul 10 at 12:40

1 Answer 1

7

What about an alias or a shell function in the appropriate start-up file?

alias mygrep='grep -r --exclude-dir=.git --exclude-dir=another_path' 
grepwithoutgitrot () { grep -r --exclude-dir=.git --exclude-dir=another_path "$@" } 

If you really want to change the grep command for your command-line needs, you could use

grep () { command grep -r --exclude-dir=.git --exclude-dir=another_path "$@" } 

... which avoids a recursion. Any of these methods would work on any unixy system with any Bourny shell (sh, bash, zsh, ksh, many others).

3
  • The alias option gets my vote. It allows the user to choose the normal grep whenever suitable. Commented Jul 10 at 10:08
  • 2
    @PeterBill you can always use command or prefix the command with `\` to over-ride any alias or function to run the original binary directly. Commented Jul 10 at 12:43
  • 1
    And you could just as easily name the function mygrep, @PeterBill. There is no benefit in using an alias, it doesn't make it any more easy (or hard, for that matter) to use the regular grep. Commented Jul 10 at 14:17

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.