Grep Command Builder

Generate copy-ready Linux grep commands for searching logs, files and directories using regular expressions, recursive searches and surrounding context.

Build your grep command

Generated command

What it does

Common grep command examples

The builder above creates the most common grep combinations. These examples show how the generated commands can be used during real log and server investigations.

Search a log file while ignoring case

grep -i "error" /var/log/app.log

Matches error, ERROR and other capitalisation variations.

Show matching line numbers

grep -n "failed" app.log

Adds the source line number to every result, making the match easier to locate in an editor or a larger log file.

Search recursively for several terms

grep -RiE "error|warning|failed" /var/log/myapp/

Searches all files beneath the directory, ignores case and uses an extended regular expression to match any of the listed terms.

Show lines surrounding a match

grep -C 3 "timeout" app.log

Shows three lines before and after each timeout, which is useful when the important cause appears immediately around the matching line.

Exclude compressed log files

grep -R --exclude="*.gz" "database connection" /var/log/

Recursively searches current log files while skipping compressed .gz archives.

Useful grep options explained

Place search patterns inside quotes so that the shell does not interpret characters such as *, $ or | before grep receives them.

Grep command FAQs

How do I search all files inside a directory?

Use grep -R "search text" /path/to/directory/. Add -i when the capitalisation of the text is unknown.

How do I search for several words with grep?

Enable extended regular expressions with -E and separate the alternatives with a pipe, for example grep -E "error|warning|failed" app.log.

How do I show lines before and after a grep match?

Use -C followed by the number of surrounding lines, such as grep -C 3 "timeout" app.log.

What is the difference between grep -r and grep -R?

Both search recursively. -R also follows symbolic links encountered during the search, while -r normally does not follow every symbolic link.

Related

Grep Cheat Sheet Grep Quiz Advanced Grep Techniques