How do you exclude symlinks in a grep?

Grep

Grep Problem Overview


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 you.

Grep Solutions


Solution 1 - Grep

Gnu grep v2.11-8 and on if invoked with -r excludes symlinks not specified on the command line and includes them when invoked with -R.

Solution 2 - Grep

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 '{}' \;

Solution 3 - Grep

For now.. here is how I would exclude symbolic links when using grep


If you want just file names matching your search:

for f in $(grep -Rl 'search' *); do if [ ! -h "$f" ]; then echo "$f"; fi; done;

Explaination:

  • grep -R # recursive
  • grep -l # file names only
  • if [ ! -h "file" ] # bash if not a symbolic link

If you want the matched content output, how about a double grep:

srch="whatever"; for f in $(grep -Rl "$srch" *); do if [ ! -h "$f" ]; then
  echo -e "\n## $f";
  grep -n "$srch" "$f";
fi; done;

Explaination:

  • echo -e # enable interpretation of backslash escapes
  • grep -n # adds line numbers to output

.. It's not perfect of course. But it could get the job done!

Solution 4 - Grep

If you're using an older grep that does not have the -r behavior described in Aryeh Leib Taurog's answer, you can use a combination of find, xargs and grep:

find . -type f | xargs grep "text-to-search-for"

Solution 5 - Grep

If you are using BSD grep (Mac) the following works similar to '-r' option of Gnu grep.

grep -OR <PATTERN> <PATH> 2> /dev/null

From man page

> -O If -R is specified, follow symbolic links only if they were explicitly listed on the command line.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDrew LeSueurView Question on Stackoverflow
Solution 1 - GrepAryeh Leib TaurogView Answer on Stackoverflow
Solution 2 - GrepJonathan HartleyView Answer on Stackoverflow
Solution 3 - GrepbksundayView Answer on Stackoverflow
Solution 4 - GrepBrenda BellView Answer on Stackoverflow
Solution 5 - GrepvissreeView Answer on Stackoverflow