1.10 Search and Discovery Utilities
find — Directory Hierarchy Search Utility
Format:
find [search_path] [matching_criteria/options] [actions]
Common Options and Matching Criteria:
Name-Based Criteria:
-
-name: Match file names (case-sensitive).
-
-iname: Match file names (case-insensitive).
-
-path: Match against the full file path.
-
-regex: Match against a regular expression pattern.
Example:
$ find /home -name "*.txt"
Type-Based Criteria:
-
-type f: Regular file.
-
-type d: Directory.
-
-type l: Symbolic link.
-
-type b: Block device.
-
-type c: Character device.
Example:
$ find /etc -type d
Size-Based Criteria:
-
-size 10k: Exactly 10 KB.
-
-size +10k: Greater than 10 KB.
-
-size -10k: Less than 10 KB.
| Size | Units |
|---|---|
| c | Bytes |
| k | KB |
| M | MB |
| G | GB |
Example:
$ find . -size +100M
Time-Based Criteria:
-
-mtime: Modification time (measured in days).
-
-atime: Access time.
-
-ctime: Metadata status change time.
Time Syntax:
-
-mtime 3: Exactly 3 days ago.
-
-mtime +3: More than 3 days ago.
-
-mtime -3: Within the last 3 days.
Example:
$ find /var/log -mtime +7
Permission and Ownership Criteria:
find . -perm 644
find . -user david
find . -group users
Logical Operators:
-
-a: Logical AND (default behavior).
-
-o: Logical OR.
-
!: Logical NOT.
Examples:
$ find . -type f -a -size +1M
$ find . -name \"\*.c\" -o -name \"\*.h\"
Common Structural Actions:
-
-print: Print the absolute or relative file path to stdout (default behavior).
-
-exec: Execute a trailing shell command on the matched targets.
$ find . -name "*.log" -exec rm {} \;
(Note: {} represents the current matched file path, and \; terminates the execution pattern).
- -delete: Remove the matched targets directly.
$ find . -name "*.tmp" -delete
- -ok: Perform interactive execution, prompting the user prior to running the command.
$ find . -name "*.log" -ok rm {} \;
Hierarchy Constraints:
-
-maxdepth n: Descend at most n levels of directories below the starting points.
-
-mindepth n: Do not apply any criteria or actions at levels less than n.
$ find . -maxdepth 1 -type f
Common Practical Invocations:
$ find /var/log -type f -mtime +7 -delete # Purge logs older than 7 days
$ find / -type f -size +1G # Locate large files across root
$ find . -type f -name "*.sh" -exec chmod +x {} \; # Batch update
script permissions
$ find . -type f | wc -l # Count the number of total files
grep — Pattern Matching and Key String Search Utility
Format:
grep [options] "pattern" [file...]
Or when processing stream inputs via pipes:
command | grep [options] "pattern"
Matching Control Options:
-
-i: Ignore case distinctions in both the pattern and the input files.
-
-v: Invert the sense of matching, selecting non-matching lines.
-
-w: Select only those lines containing matches that form whole words.
-
-x: Select only those matches that exactly match the whole line.
-
-e: Specify multiple search patterns.
-
-f: Obtain patterns from the specified file, one per line.
Examples:
$ grep -i \"error\" log.txt
$ grep -v \"debug\" log.txt
$ grep -w \"main\" test.c
Display Control Options:
-
-n: Prefix each line of output with the 1-based line number within its input file.
-
-c: Suppress normal output; instead print a count of matching lines for each input file.
-
-l: Suppress normal output; instead print the name of each input file from which output would normally have been printed.
-
-L: Suppress normal output; instead print the name of each input file from which no output would normally have been printed.
-
-o: Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
-
--color=auto: Highlight the matching string fragments using ANSI escape sequences.
Examples:
$ grep -n \"error\" log.txt
$ grep -c \"error\" log.txt
Recursive Directory Traversal Options:
-
-r: Read all files under each directory, recursively.
-
-R: Read all files under each directory, recursively, and follow all symbolic links.
Example:
$ grep -r \"main\" .
Context Display Options:
-
-A n: Print n lines of trailing context after matching lines.
-
-B n: Print n lines of leading context before matching lines.
-
-C n: Print n lines of leading and trailing output context.
Example:
$ grep -C 3 \"error\" log.txt
Regular Expression Variants:
(Note: Basic Regular Expressions (BRE) are supported by default).
-
-E: Interpret the pattern as an Extended Regular Expression (ERE) (equivalent to egrep).
-
-F: Interpret the pattern as a fixed string, suppressing regular expression parsing (equivalent to fgrep).
-
-P: Interpret the pattern as a Perl-Compatible Regular Expression (PCRE) (on supported environments).
Examples:
$ grep -E \"error\|fail\" log.txt
$ grep -F \"a+b\" file.txt
Common Practical Invocations:
$ grep -i "error" /var/log/syslog # Search system logs for error tags
$ grep -rn "main" . # Recursively locate functions in source trees
$ grep -c "error" log.txt # Calculate total instances of matches
$ ps aux | grep nginx # Filter active process listings
$ grep -o "[0-9]\+" file.txt # Extract matched segments exclusively
Regular Expression Fundamentals:
| Expression | Matching Pattern |
|---|---|
| . | Matches any single character. |
| * | Matches the preceding element zero or more times. |
| ^ | Anchors the match to the start of a line. |
| $ | Anchors the match to the end of a line. |
| [abc] | Matches a bracket expression containing a, b, or c. |
| [^abc] | Matches any single character not matching a, b, or c. |
Examples:
grep "^root" /etc/passwd
grep "[0-9]\{3\}" file.txt
Historical Implementations vs. Modern Standards:
-
grep: Basic Regular Expressions (BRE).
-
egrep: Extended Regular Expressions (ERE), deprecated in favor of grep -E.
-
fgrep: Fixed String matching, deprecated in favor of grep -F.
Modern infrastructure favors explicitly passing the flag vectors:
grep -E
grep -F