Search
The grep command
Searches a pattern inside files and shows only the matching lines.
Most useful options
-iIgnores upper and lower case-vShows the lines that do NOT match-nNumbers the lines-rSearches inside every file of a folder-cCounts matching lines instead of showing them-wWhole word only-EPattern as an extended regular expression (egrep equivalent)Copy-ready examples
$ grep mkdir Documentos/tareas.txtSearch in a file$ grep -i bash Documentos/apuntes.txtCase-insensitive$ grep -rn bash .In the whole folder, with numbers$ grep -c root /etc/passwdCount matchesYou can try them in the browser terminal: it does not touch your computer.
Typical mistakes
- By default it is case-sensitive.
FAQ
Does grep search files or text?
It searches text inside files. To find files by name you use find.
How do I count the matches?
Pipe it to wc -l: grep -r word . | wc -l.
More Search commands
Learn to actually use it
The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.
Start the free course