Search
The fgrep command
Searches literal text, with no special characters. Equivalent to grep -F.
Most useful options
-i, --ignore-caseIgnores upper and lower case-v, --invert-matchShows the lines that do not contain the text-c, --countPrints only how many lines match-n, --line-numberPrefixes each match with its line number-w, --word-regexpOnly accepts matches that are whole words-x, --line-regexpRequires the whole line to match the textCopy-ready examples
$ fgrep root /etc/passwdlines containing the string root.$ fgrep '1.5' precios.txtmatches the dot as a dot. With grep, the dot would match any character.$ fgrep -i -n 'TODO' src/*.tsevery TODO in the TypeScript files, ignoring case.$ fgrep -v -x '' lista.txtremoves empty lines: -x requires the whole line and '' is the empty line.You can try them in the browser terminal: it does not touch your computer.
Common errors
| Message | Cause | Fix |
|---|---|---|
| fgrep: warning: fgrep is obsolescent; using grep -F | Since grep 3.8, fgrep warns that it is obsolete and still runs the search. Depending on the distribution, the warning may not appear. | Replace fgrep with grep -F. |
| grep: noexiste: No such file or directory | The file does not exist or is misspelled. | Check the name with ls. |
Typical mistakes
- The GNU grep manual considers fgrep obsolete and recommends grep -F. In new scripts, use grep -F.
- With -x, the string must fill the whole line. Without -x, it only has to appear somewhere in the line.
- If the text starts with a dash, grep takes it as an option. Use -e before the text, or -- to end the options.
Differences from similar commands
FAQ
When do I use fgrep instead of grep?
When the string contains dots, asterisks or brackets that you want to match as they are, such as an IP, a path or a code fragment.
Can I search for several strings at once?
Yes, by repeating -e: fgrep -e root -e bin /etc/passwd returns the lines that contain either one.
Should I change my fgrep scripts to grep -F?
Yes. POSIX removed it in 2001, GNU grep treats it as obsolete and since version 3.8 warns when you use it. grep -F does the same.
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