Subject: bug#47649: grep bug report - improper handling of file symlinks with -r option

From: Jim Meyering <[email-masked]>
Date: Fri, 6 Aug 2021 22:10:22 -0700

tags 47649 notabug
close 47649
done

On Wed, Apr 7, 2021 at 7:21 PM Chris Drake <[email-masked]> wrote:
> *This is the original working grep behaviour - it found text inside files
> that were symlinks:-*
> (Note the output: "*./folder/testfile1:this is my test file*")
>
> [root@ir2 ~]# grep --version
> grep (GNU grep) 2.5.1
>
> Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.
> This is free software; see the source for copying conditions. There is NO
> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>
> [root@ir2 ~]# mkdir bugdemo
> [root@ir2 ~]# cd bugdemo
> [root@ir2 bugdemo]# echo "this is my test file" > testfile1
> [root@ir2 bugdemo]# echo "this is my test file" > testfile2
> [root@ir2 bugdemo]# mkdir folder
> [root@ir2 bugdemo]# cd folder
> [root@ir2 folder]# echo "this is my test file" > testfile3
> [root@ir2 folder]# ln -s ../testfile1
> [root@ir2 folder]# cd ..
> [root@ir2 bugdemo]# unalias grep
> [root@ir2 bugdemo]# grep -r test .
> ./testfile2:this is my test file
> ./folder/testfile1:this is my test file
> ./folder/testfile3:this is my test file
> ./testfile1:this is my test file
> [root@ir2 bugdemo]#
...
> It looks like processing exists to not recursively follow symlinks, and
> someone has messed with that which has caused files to no longer be
> searched by mistake.

Thanks for the report, but that is the documented behavior of -r.
You appear to prefer -R:

‘-r’
‘--recursive’
     For each directory operand, read and process all files in that
     directory, recursively.  Follow symbolic links on the command line,
     but skip symlinks that are encountered recursively.  Note that if
     no file operand is given, grep searches the working directory.
     This is the same as the ‘--directories=recurse’ option.

‘-R’
‘--dereference-recursive’
     For each directory operand, read and process all files in that
     directory, recursively, following all symbolic links.