Commands for searching files, directories, and text content.
Searches for files and directories that match specified criteria.
Syntax/Option | Description |
---|---|
find [path] -name [pattern] | Search for files with names matching the pattern |
find [path] -iname [pattern] | Search for files with names matching the pattern (case-insensitive) |
find [path] -type f | Search for regular files only |
find [path] -type d | Search for directories only |
find [path] -size [+/-]Nc | Search for files of specific size (N=size, c=unit, +/-=larger/smaller than) |
find [path] -mtime [+/-]N | Search for files modified N days ago |
find [path] -user [username] | Search for files owned by specific user |
find [path] -perm [mode] | Search for files with specific permissions |
find [path] -exec [command] {} \; | Execute command on each found file |
Examples:
find /home -name "*.txt"
- Find all .txt files in /home directoryfind . -type f -size +10M
- Find files larger than 10MB in current directoryfind /var/log -name "*.log" -mtime -7
- Find log files modified in the last 7 daysfind . -type f -name "*.jpg" -exec cp {} /backup/ \;
- Copy all JPG files to backup directoryfind . -type f -empty
- Find empty filesfind . -type f -perm 0777 -print
- Find files with 777 permissions (readable/writable/executable by all users)find . -type f -not -name "*.txt" -not -name "*.log"
- Find files that are not text or log filesfind . -type f -name "*.log" -size +1M -exec ls -lh {} \; | sort -k5,5hr
- List log files larger than 1MB sorted by sizefind . -type f -name "*.conf" -exec grep -l "DEBUG" {} \;
- Find configuration files containing "DEBUG"find . -type f -mtime +30 -name "*.bak" -delete
- Delete backup files older than 30 days
Quickly searches for files and directories using a pre-built database.
Option | Description |
---|---|
-i | Case-insensitive search |
-l, --limit N | Limit results to N entries |
-c, --count | Display only the number of matching files |
-r, --regexp | Search using regular expressions |
-S, --statistics | Display database statistics |
Examples:
locate "*.conf"
- Find all .conf fileslocate -i "readme"
- Find files containing "readme" (case-insensitive)locate -c "*.jpg"
- Count the number of JPG filessudo updatedb
- Update the locate database (run before searching for most recent results)locate -r "\.mp4$"
- Find MP4 files using regular expressionlocate -l 10 "*.pdf"
- Show only first 10 PDF fileslocate -e "report"
- Show only existing files (exclude files deleted since database update)locate "*.log" | grep "/var/" | xargs ls -lh
- Show detailed information for log files in /var directory
Note: locate uses a database for fast searching, but since the database is updated periodically, recently created files might not appear in search results. To search with the most up-to-date information, run sudo updatedb
to update the database.
Shows the full path of commands by searching directories in the PATH environment variable.
Option | Description |
---|---|
-a | Display all matching executables in PATH (default shows only the first match) |
Examples:
which python
- Show location of python executablewhich -a python
- Show all python executables in PATHwhich gcc g++ make
- Show locations of multiple commands at oncewhich -a java | xargs ls -l
- Show detailed information for all java executableswhich $SHELL
- Show location of current shell
Shows the locations of a command's binary, source code, and manual pages.
Option | Description |
---|---|
-b | Search for binary files only |
-m | Search for manual pages only |
-s | Search for source files only |
Examples:
whereis ls
- Show locations of ls command binary, source, and manualwhereis -b python
- Show only binary locations for pythonwhereis -m gcc
- Show only manual page locations for gccwhereis -s bash
- Show only source file locations for bashwhereis -B /usr/bin -f ls
- Search for ls command in specific directoryfor cmd in ls cp mv rm; do whereis $cmd; done
- Show locations for multiple commands at once
Searches for text patterns in files. This is a more detailed explanation than in the Text Processing section.
Option | Description |
---|---|
-i | Case-insensitive search |
-v | Display lines that don't match the pattern |
-n | Display line numbers |
-r, -R | Search recursively through directories |
-l | Display only filenames containing matches |
-c | Display count of matching lines |
-w | Match whole words only |
-E | Use extended regular expressions |
-F | Treat pattern as fixed string (disable regular expressions) |
-A n | Display n lines after the match |
-B n | Display n lines before the match |
-C n | Display n lines before and after the match |
--color | Highlight matching text with color |
Examples:
grep "error" /var/log/syslog
- Search for "error" in sysloggrep -i "warning" *.log
- Search for "warning" in all log files (case-insensitive)grep -r "TODO" --include="*.py" .
- Recursively search for "TODO" in Python filesgrep -v "^#" config.conf
- Display lines that don't start with # (exclude comments)grep -E "error|warning" log.txt
- Search for lines containing "error" or "warning"grep -n "function" script.js
- Show line numbers for lines containing "function"grep -o "IP: [0-9.]\+" server.log
- Extract and display only IP addressesgrep -A 2 -B 1 "ERROR" application.log
- Show error lines with contextgrep -l "TODO" *.py | xargs wc -l
- Count lines in Python files containing "TODO"history | grep "git commit"
- Search command history for git commitsps aux | grep "[n]ginx"
- Search for nginx processes (excluding grep process itself)
ack and ag (The Silver Searcher) are grep alternatives optimized for programmers.
Command/Option | Description |
---|---|
ack [pattern] | Search for pattern in files in current directory |
ack --[language] | Search only in files of specific language |
ack -i | Case-insensitive search |
ack -l | Display only filenames containing matches |
ag [pattern] | Similar to ack but faster |
ag --ignore-dir=[dir] | Ignore specific directory |
ag --depth=[n] | Limit search depth |
Examples:
ack "function"
- Search for "function" in files in current directoryack --js "setTimeout"
- Search for "setTimeout" in JavaScript filesag "import"
- Quickly search for "import" in files in current directoryag --python "def main"
- Search for "def main" in Python filesack -i "todo" --ignore-dir=vendor/
- Search for "todo" case-insensitively, excluding vendor directoryack -w "log" --type=ruby
- Search for whole word "log" in Ruby filesag -G "\.java$" "public class"
- Search for "public class" in Java filesag -l "TODO|FIXME" | xargs wc -l
- Count lines in files containing "TODO" or "FIXME"ag --count "import" --sort-files
- Show count of "import" occurrences in each file, sorted by filename
Note: ack and ag ignore version control directories (.git, .svn, etc.) and temporary files by default, and only search text files. This makes them more efficient than grep for searching code.
Combining find and grep allows you to search for text in specific file types.
Examples:
find . -name "*.py" -exec grep "import" {} \;
- Search for "import" in Python filesfind . -name "*.log" -mtime -7 -exec grep "ERROR" {} \;
- Search for "ERROR" in log files modified in the last 7 daysfind . -type f -not -path "*/\.*" -exec grep -l "TODO" {} \;
- Find files containing "TODO", excluding hidden filesfind . -name "*.conf" -exec grep -l "DEBUG" {} \; | xargs cp -t /backup/debug-configs/
- Copy configuration files containing "DEBUG" to backup directoryfind . -name "*.java" -exec grep -l "Deprecated" {} \; | xargs sed -i 's/Deprecated/DEPRECATED/g'
- Batch edit Java files containing "Deprecated"find . -type f -size +1M -exec grep -l "ERROR" {} \; | xargs ls -lh
- Show details of large files containing "ERROR"find . -name "*.log" -exec grep -q "FATAL" {} \; -print
- Show only paths of log files containing "FATAL"find . -name "*.sh" -type f -exec grep -l "#!/bin/bash" {} \; | xargs chmod +x
- Make all bash scripts executable