brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: grep -F searches for text literally, dots and brackets included

Search
~/tips/tip-grep-fixed-strings

grep -F '192.168.1.1' /var/log/nginx/access.log

Searches for text exactly as you typed it, without regex characters getting in the way.

The dot in 192.168.1.1 looks innocent, but to grep it’s a regular expression that means “any character”. So searching for that IP also matches 192x168y1z1, and a search for something with brackets or a * in it can go completely sideways.

-F (long form --fixed-strings) tells grep to treat the pattern as plain text, not as a regular expression:

grep -F '192.168.1.1' /var/log/nginx/access.log

Every character now means itself. No escaping of dots, no surprises with [, * or $. You can see the difference with a tiny test:

printf '192.168.1.1\n192x168y1z1\n' | grep -F 192.168.1.1

With -F you get one line back. Without it, both lines match.

I’d reach for -F whenever the thing I’m searching for was copied from somewhere: an IP, a file path, an error message with parentheses in it, a URL with a ?. It also combines nicely with -f file, which reads one pattern per line from a file, so you can search for a whole list of literal strings at once.

The caveat is that you lose everything regex can do. ^ and $ no longer anchor the match, and 192.168.1.1 still matches inside 192.168.1.10. If that matters, add -w or -x, which work with fixed strings too.

Further reading: grep(1) manual page, GNU grep manual

Going deeper: Run degoog on Unraid as your own search page, compared with SearXNG and 4get

More Search tips !!wfr Wau Holland zipgrep db_host backup.zip pdfgrep -rin "warranty" ~/Documents "fatal error" repo:github-linguist/linguist is:open involves:@me All Search tips (11) All topics

Link