Appearance
Searching with grep, find, and awk
When working with large projects or simulation data, you’ll often need to search for specific files, text, or patterns within directories or output logs. Linux provides three powerful commands — grep, find, and awk — to make searching fast and precise.
grep – Search Inside Files
grep searches for a keyword or pattern inside files.
| Command | Description | Example |
|---|---|---|
grep "text" file.txt | Finds lines containing "text" in a file | grep "error" output.log |
grep -r "keyword" folder/ | Searches recursively in all files | grep -r "mesh" ./codes/ |
grep -n "pattern" file.txt | Shows line numbers with matches | grep -n "force" beam_solver.py |
find – Locate Files and Directories
find searches your file system for files or folders matching given conditions.
| Command | Description | Example |
|---|---|---|
find . -name "filename" | Search by name in current directory | find . -name "mesh.py" |
find /path -type f -name "*.txt" | Search all .txt files | find ~/projects -type f -name "*.py" |
find . -mtime -1 | Find files modified in the last 24 hours | find . -mtime -1 |
awk – Filter and Process Text
awk is a mini text processor — great for analyzing columns or structured data (like CSV or logs).
| Command | Description | Example |
|---|---|---|
awk '{print $1}' file.txt | Prints the first column | awk '{print $1}' results.csv |
awk '/pattern/ {print $0}' file.txt | Prints lines matching a pattern | awk '/Error/ {print $0}' output.log |
awk -F, '{print $2, $3}' file.csv | Uses a comma as a field separator | awk -F, '{print $2, $3}' data.csv |
Summary
grep– searches inside files.find– locates files and directories.awk– filters and processes structured text.
These tools are essential for quickly navigating and analyzing simulation files, log outputs, and data sets directly from the terminal.