Back to Blog

Linux Terminal: grep vs find | Search Commands Explained

Sandy LaneSandy Lane

Video: Linux Terminal: grep vs find | Search Commands Explained by Taught by Celeste AI - AI Coding Coach

Watch full page →

Linux Terminal: grep vs find | Search Commands Explained

The Linux terminal offers powerful tools for searching, but grep and find serve distinct purposes. Grep searches for text patterns inside files, while find locates files or directories by name or type. Understanding when and how to use each command will greatly improve your efficiency in navigating and managing files.

Code

# Search for the word "error" inside a file named log.txt
grep 'error' log.txt

# Case-insensitive search for "warning" inside all .log files
grep -i 'warning' *.log

# Search for "TODO" inside a file and show line numbers where it appears
grep -n 'TODO' script.sh

# Find all files with .txt extension in the current directory and subdirectories
find . -name '*.txt'

# Find only directories within the current directory tree
find . -type d

# Find only regular files within the current directory tree
find . -type f

Key Points

  • Use grep to search for specific text patterns inside files.
  • Use find to locate files or directories by name, type, or other attributes.
  • grep -i enables case-insensitive searches, and grep -n shows line numbers.
  • find -name searches files by name pattern, while find -type filters by file or directory.
  • Remember: grep = search inside files; find = search for files.