28

I'm grepping a local svn directory. When I run grep -r "pattern" . I get some errors such as

grep: ./Data/test: No such file or directory

Who asked grep to look for non-existent files?


>grep --version
grep (GNU grep) 2.10

>lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 12.04 LTS
devnull
  • 118,548
  • 33
  • 236
  • 227
user13107
  • 3,239
  • 4
  • 34
  • 54
  • 1
    What is `./Data/test`? grep can spit out this type of error when it encounters a symbolic link that points to a non-existent file. – j.w.r Oct 12 '12 at 03:35
  • @j.w.r seems like it's a broken symbolic link (as per file command). – user13107 Oct 12 '12 at 04:12
  • Duplicate of http://stackoverflow.com/questions/6426363/how-can-i-have-grep-not-print-out-no-such-file-or-directory-errors – GlennG Jan 25 '17 at 11:16

1 Answers1

30

By default, grep would not ignore non-existent or unreadable files. You need to supply the -s or --no-messages option in order to do so. Quoting from man grep:

   -s, --no-messages
          Suppress  error  messages about nonexistent or unreadable files.
          Portability note: unlike GNU grep, 7th Edition Unix grep did not
          conform to POSIX, because it lacked -q and its -s option behaved
          like GNU grep's -q option.  USG-style grep also  lacked  -q  but
          its  -s  option  behaved  like GNU grep.  Portable shell scripts
          should avoid both -q and -s and  should  redirect  standard  and
          error output to /dev/null instead.  (-s is specified by POSIX.)
devnull
  • 118,548
  • 33
  • 236
  • 227
  • 6
    An example of non-existent would be a broken symlink. Unreadable, as the word suggests, refers to the files for which the current user doesn't have read permissions. – devnull May 01 '13 at 07:32
  • This seems to be the only solution once grep doesn't work with `2>/dev/null` – DimiDak Jun 29 '21 at 20:44