Skip to content

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.

CommandDescriptionExample
grep "text" file.txtFinds lines containing "text" in a filegrep "error" output.log
grep -r "keyword" folder/Searches recursively in all filesgrep -r "mesh" ./codes/
grep -n "pattern" file.txtShows line numbers with matchesgrep -n "force" beam_solver.py

find – Locate Files and Directories

find searches your file system for files or folders matching given conditions.

CommandDescriptionExample
find . -name "filename"Search by name in current directoryfind . -name "mesh.py"
find /path -type f -name "*.txt"Search all .txt filesfind ~/projects -type f -name "*.py"
find . -mtime -1Find files modified in the last 24 hoursfind . -mtime -1

awk – Filter and Process Text

awk is a mini text processor — great for analyzing columns or structured data (like CSV or logs).

CommandDescriptionExample
awk '{print $1}' file.txtPrints the first columnawk '{print $1}' results.csv
awk '/pattern/ {print $0}' file.txtPrints lines matching a patternawk '/Error/ {print $0}' output.log
awk -F, '{print $2, $3}' file.csvUses a comma as a field separatorawk -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.