Linux Commands - grep (--exclude-dir)

by
Jeremy Canfield |
Updated: November 05 2020
| Linux Commands articles
The grep command with the -R or --dereference-recursive flag and -e or --regexp option can be used to search every file below a certain directory for a string of data. In this example, every file below the /opt directory will be searched for the text 'foo'.
grep -R --regexp 'foo' /opt
Often, the -i or --ignore-case flag is used to make the search case insensitive.
grep -R --ignore-case --regexp 'foo' /opt
Additionally, the --exclude-dir option can be used to exclude a diretory from being searched. In this example, every file below the /opt directory will be searched for the text 'foo' except that the /opt/logs directory will be excluded from the search.
grep -R --exclude-dir=logs --ignore-case --regexp 'foo' /opt
Did you find this article helpful?
If so, consider buying me a coffee over at