Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between grep -r and -R

Tags:

grep

In the man page:

-r

Read all files under each directory, recursively, following symbolic links only if they are on the command line.

what exactly does "being on the command line" means?

Thanks.

like image 809
Josh Wang Avatar asked Mar 31 '14 13:03

Josh Wang


People also ask

What is difference between grep fgrep and egrep?

egrep is equivalent to grep -E command and it allows the use of extended regex. fgrep is equivalent to grep -F command and it uses a fixed string for search and hence it performs a faster search.

What is the difference between grep and?

The main difference between grep and egrep is that grep is a command that allows searching content according to the given regular expression and displaying the matching lines while egrep is a variant of grep that helps to search content by applying extended regular expressions to display the machining lines.

What is difference between grep egrep find and sed?

The sed command is a stream editor that works on streams of characters. It's a more powerful tool than grep as it offers more options for text processing purposes, including the substitute command, which sed is most commonly known for.

What is the difference between awk and grep?

Grep is used for finding text patterns in a file and is the simplest of the three. Sed can find and modify data, however, its syntax is a bit more complex than grep. AWK is a full-fledged programming language that can process text and perform comparison and arithmetic operations on the extracted text.


1 Answers

"Being on the command line" refers to the arguments passed to grep. If you give a symbolic link as an argument to grep -r it follows it. However, if grep -r encounters a symbolic link while traversing a directory it does not follow it (in contrast to grep -R which does).

Imagine you have a directory with a bunch of files in it, including one containing a symbolic link to .. (the parent directory):

$ ls -la
total 0
drwxr-xr-x 2 aw aw 47 Mar 31 16:05 .
drwxr-xr-x 3 aw aw 27 Mar 31 16:04 ..
-rw-r--r-- 1 aw aw  0 Mar 31 16:05 bar
-rw-r--r-- 1 aw aw  0 Mar 31 16:05 baz
lrwxrwxrwx 1 aw aw  2 Mar 31 16:04 foo -> ..
-rw-r--r-- 1 aw aw  0 Mar 31 16:05 quux
$

Then,

  • grep -r foobar . will only grep the files inside this directory,
  • grep -r foobar foo will grep the files in the parent directory (..) (following the symlink given as an argument),
  • grep -R foobar . will also grep the files in the parent directory (following the symlink not given as an argument but found while traversing the current directory).
like image 170
Andreas Wiese Avatar answered Sep 21 '22 06:09

Andreas Wiese