Searching through files for specific text is one of the most routine tasks on any Linux system — whether you’re scanning logs for errors, auditing configuration files, or tracing a string across a large codebase. Linux has several tools for this. Some are built-in and fast; others give you column-level filtering and in-place editing. This guide covers grep, find, awk, sed, and ripgrep, with practical examples for each.
How to Find Text in Files in Linux with grep
grep (Global Regular Expression Print) reads files line by line and returns every line matching a given pattern. It ships on every major Linux distribution and handles the majority of day-to-day text search tasks without any setup.
Basic grep Syntax and Key Flags
The standard form takes a search term and a filename:
grep "error" logfile.txt
That returns every line in logfile.txt containing “error.” The most useful flags:
| Flag | Purpose | Example |
|---|---|---|
-i | Case-insensitive match | grep -i "warning" logfile.txt |
-n | Show line numbers in output | grep -n "fail" system.log |
-v | Exclude lines that match | grep -v "debug" logfile.txt |
-w | Whole word matching only | grep -w "root" /etc/passwd |
-r | Recurse into all subdirectories | grep -r "keyword" /var/logs/ |
grep also accepts regular expression patterns natively. Running grep "error[0-9]" logfile.txt matches “error1”, “error2”, and so on. Extended regex is available with the -E flag.
Recursive grep to Find Text Across Linux Directories
The -r flag walks every file in a directory tree:
grep -r "error" /var/logs/
Combine it with -i for case-insensitive recursive search:
grep -ri "error" /var/logs/
Filtering grep Search by File Type in Linux
When a directory holds mixed file types, --include and --exclude narrow the scope:
grep -r --include="*.log" "error" /var/logs/ grep -r --exclude="*.bak" "error" /var/logs/ grep -r --exclude-dir="backup" "error" /var/logs/
Combining find and grep to Linux Find Text in Files by Path or Type
grep’s --include flag covers straightforward cases. Pairing it with find adds filters that grep alone cannot handle — file size, modification time, ownership, and nested path patterns. When you need to locate files in Linux by name or metadata first, then search their contents, find handles the selection and passes results to grep via -exec.
find /var/logs -type f -name "*.log" -exec grep "error" {} +
Search for two terms at once across all file types:
find /var/logs -type f -exec grep -E "error|failed" {} +
Skip a specific subdirectory entirely:
find /var/logs -type f -not -path "*/backup/*" -exec grep "error" {} +
Ignore a file extension:
find /var/logs -type f -not -name "*.bak" -exec grep "error" {} +
awk and sed: Searching and Transforming Text in Linux Files
grep outputs matching lines as-is. For column-level extraction, conditional logic, or search-and-replace across a stream, awk and sed are the right tools.
awk matches a pattern and lets you act on individual fields:
awk '/error/ {print $2}' logfile.txt
awk 'tolower($0) ~ /error/' logfile.txt
awk '/error|fail/' logfile.txt
sed handles stream-level substitution and range output:
sed 's/error/warning/g' logfile.txt sed -n '/error/,+2p' logfile.txt
The first sed command replaces every occurrence of “error” with “warning.” The second prints the matched line plus the two that follow it.
Whole Word Matching When You Find Text in Linux Files
Without -w, grep matches substrings. Searching for “root” also returns “rooted” and “root123.” The flag prevents partial matches:
grep -w "root" /etc/passwd
Case-insensitive whole-word match:
grep -wi "error" logfile.txt
For stricter boundary control with extended regex:
grep -E "\broot\b" /etc/passwd
Showing Line Numbers and Context When grep Finds Text in Linux
In large files, seeing surrounding lines alongside a match saves a lot of back-and-forth. Four flags cover this:
grep -n "failed" auth.log grep -C 3 "error" system.log grep -B 2 "timeout" server.log grep -A 4 "disk failure" hardware.log
-C 3 shows three lines before and after every match. -B and -A control just one side. You can also pipe live process output directly into grep to filter results from running commands without writing to disk.
Faster Linux Text Search with ripgrep
ripgrep (rg) is a modern recursive search tool written in Rust. It skips binary files and hidden directories by default, respects .gitignore rules, and runs significantly faster than standard grep on large directory trees.
Install it using the apt package manager on Debian/Ubuntu:
sudo apt install ripgrep
On RHEL/CentOS:
sudo yum install ripgrep
Basic usage is close to grep:
rg "error" /var/logs/ rg -i "warning" rg -w "root" rg -n -C 3 "error" /var/logs/
If rg returns “command not found” after install, confirm the binary is in your PATH before running it again.
Full Method Comparison: Linux Find Text in Files
| Method | Best Use Case | Key Flags |
|---|---|---|
grep | Single file or basic pattern match | -i -w -n -v |
grep -r | Whole directory tree, quick search | -r -i --include |
find + grep | File-type or path-filtered search | -exec grep |
awk | Column extraction and reformatting | /pattern/ {print $N} |
sed | Search and replace in stream | s/old/new/g |
ripgrep (rg) | Fast recursive search, large codebases | -n -C -w -i |
FAQs
What does grep stand for in Linux?
grep stands for Global Regular Expression Print. It scans input files line by line and returns every line matching the given pattern. The name comes from the ed editor’s g/re/p command.
How do I search for text recursively across all Linux directories?
Use the -r flag: grep -r "search_term" /path/to/dir/. Add -i for case-insensitive results. To limit to specific file types, append --include="*.log" to the same command.
How do I find text in a specific file type in Linux?
Two approaches work: grep -r --include="*.log" "term" /dir/ or find /dir -name "*.log" -exec grep "term" {} +. The find approach gives more control over file filtering criteria.
What is the difference between grep and ripgrep for text search in Linux?
ripgrep is faster on large directory trees, skips binary and hidden files by default, and respects .gitignore rules automatically. grep is available everywhere without install and is sufficient for most single-file or small-directory tasks.
How do I show lines before and after a grep match in Linux?
Use -C N for N lines on both sides, -B N for lines before, and -A N for lines after. Example: grep -C 3 "error" system.log shows three surrounding lines with each match.