File Search and grep¶
Finding files by name/attributes with find, searching file contents with grep, and fast lookups with locate. These tools form the core of filesystem exploration and log analysis.
find - Search by File Attributes¶
Name Matching¶
find . -name "file.txt" # exact name (case-sensitive)
find . -iname "file.txt" # case-insensitive
find . -name "*.txt" # wildcard: all .txt files
find . -name "report??" # ? = exactly one character
find ~ -name "*.sh" # search in home directory
find /var/log -name "*.log" # search specific directory
Type Filtering¶
find . -type f # regular files only
find . -type d # directories only
find . -type l # symbolic links only
Size Filtering¶
find . -size +10k # larger than 10 KB
find . -size -1M # smaller than 1 MB
find . -size +100M # larger than 100 MB
Size suffixes: c (bytes), k (KB), M (MB), G (GB)
Depth Control¶
Combined Conditions¶
Execute Actions on Results¶
find . -name "*.tmp" -delete # delete matches
find . -name "*.sh" -exec chmod +x {} \; # chmod each found file
grep - Search File Contents¶
Common Flags¶
| Flag | Meaning |
|---|---|
-r | Recursive search |
-i | Case-insensitive |
-n | Show line numbers |
-v | Invert match (lines NOT matching) |
-l | Show only filenames |
-c | Count matching lines |
-E | Extended regex (same as egrep) |
--include="*.py" | Filter by file type |
Examples¶
grep "error" file.txt # single file
grep -r "error" /var/log/ # recursive in directory
grep -rin "error" . # recursive, case-insensitive, line numbers
grep -rin --include="*.py" "error" . # only Python files
grep -v root /etc/passwd # lines NOT containing "root"
Pipeline Filtering¶
cat /var/log/syslog | grep 'ssh' | grep -v root
cat file | grep 'pattern' | wc -l # count matching lines
command | grep 'keyword' # filter any command output
Other Search Tools¶
locate - Fast Database Search¶
Note: locate uses a pre-built database that may be outdated. Run updatedb to refresh.
whereis - Find Binary and Docs¶
which - Find Executable in PATH¶
Patterns¶
Find Large Files¶
Search and Replace in Files¶
Find Recently Modified Files¶
find . -type f -mtime -1 # modified in last 24 hours
find . -type f -mmin -60 # modified in last 60 minutes
Gotchas¶
findwildcards must be quoted ("*.txt") or the shell expands them before find sees themlocatemay return stale results if database is old - alwaysupdatedbfirstgrep -rfollows symlinks by default - use-R(capital) to not follow on some systemsfind . -name ???matches any 3-character filename (not just digits)- Redirect stderr when searching from root:
find / -name "file" 2>/dev/null
See Also¶
- [[text-processing]] - awk, sed, sort, uniq for post-processing search results
- [[io-redirection-and-pipes]] - Piping grep output to other commands
- [[logging-and-journald]] - Searching log files effectively