grep: dir: Is a directory
- tool
- grep
- exit code
- 2
- versions
- GNU grep 3.x
- os
- linux, macos, windows
grep: src: Is a directory means one of the arguments you passed to grep is a directory, and without -r GNU grep reads only files. Use grep -r PATTERN src to search every file under it. grep still searches the other arguments, but it exits with status 2 because an error occurred, even when other files matched, so scripts treat the run as failed.
Reproduce the error
mkdir src
echo "TODO: fix" > src/app.js
grep TODO srcOutput · exit 2
grep: src: Is a directory
With a glob, the directory is reported and the files are still searched:
mkdir -p project/src && cd project
echo "TODO: write docs" > notes.txt
grep TODO *Output · exit 2
notes.txt:TODO: write docs grep: src: Is a directory
The match in notes.txt is printed, but the exit status is 2. GNU grep returns 2 whenever an error occurred, unless -q is used and a line was selected. -s (--no-messages) hides the message but keeps status 2.
Fix: search recursively
mkdir src
echo "TODO: fix" > src/app.js
grep -r TODO src
grep -rn --include='*.js' TODO .Output
src/app.js:TODO: fix ./src/app.js:1:TODO: fix
-r follows symlinks only for the paths on the command line; -R follows all of them.
Fix: skip directories
-d ACTION (--directories=ACTION) controls what grep does with a directory argument: read (the default, which produces this message), skip, or recurse (same as -r):
mkdir -p project/src && cd project
echo "TODO: write docs" > notes.txt
grep -d skip TODO *
echo "exit status: $?"Output
notes.txt:TODO: write docs exit status: 0