How do you exclude symlinks in a grep?

后端 未结 4 889
梦如初夏
梦如初夏 2020-12-30 18:36

I want to grep -R a directory but exclude symlinks how dow I do it?

Maybe something like grep -R --no-symlinks or something?

Thank

4条回答
  •  一个人的身影
    2020-12-30 19:08

    If you already know the name(s) of the symlinks you want to exclude:

    grep -r --exclude-dir=LINK1 --exclude-dir=LINK2 PATTERN .
    

    If the name(s) of the symlinks vary, maybe exclude symlinks with a find command first, and then grep the files that this outputs:

    find . -type f -a -exec grep -H PATTERN '{}' \;
    

    The '-H' to grep adds the filename to the output (which is the default if grep is searching recursively, but is not here, where grep is being handed individual file names.)

    I commonly want to modify grep to exclude source control directories. That is most efficiently done by the initial find command:

    find . -name .git -prune -o -type f -a -exec grep -H PATTERN '{}' \;
    

提交回复
热议问题