In the last lesson, grep ERROR ~/logs failed with Is a directory. The flag -r makes grep recursive, the same as cp -r and rm -r: it reads every file under the directory, at all levels.
learner@tidepool:~$ grep -r host conf
conf/a.conf:host=db1
conf/sub/b.conf:host=db2- conf← start here
- a.conf← read
- sub
- b.conf← read
Each line starts with the path of the file, the same as with more than one file.
-l: only the file names
When you want to know where something is, the lines are too much. -l writes each matching file one time:
learner@tidepool:~$ grep -rl host conf
conf/a.conf
conf/sub/b.confThis is the first step when a setting must change: get the list of files that have it.
| Line | Writes |
|---|---|
grep -r TEXT DIR | Each matching line, after its path |
grep -rn TEXT DIR | The same, with line numbers |
grep -rl TEXT DIR | Only the paths of the files that match |
grep -ril TEXT DIR | The same, for any uppercase or lowercase |
Files that you cannot read
Some files are closed to your account. grep writes Permission denied for each one and continues. The chapter "File Permissions" explains why.