1. Detailed Explanation of the find Command
1.1 Purpose
<span>find</span> is the most powerful file search tool in Linux, capable of recursively searching for files in a directory tree. It supports filtering by various criteria such as name, type, size, and time, and can perform various operations on the search results.
1.2 Syntax
find [path] [options] [expression]
Common options:
<span>-name</span>: Search by file name (case-sensitive)<span>-iname</span>: Search by file name (case-insensitive)<span>-type</span>: Search by file type (f=regular file, d=directory)<span>-size</span>: Search by file size<span>-mtime</span>: Search by modification time<span>-exec</span>: Execute a command on the search results
1.3 Examples
Search by name:
# Exact file search
find /home -name "project.txt"
# Case-insensitive search
find . -iname "readme.md"
# Use wildcard to find all .c files
find /home -name "*.c"
Search by type:
# Find directories
find /var -type d -name "log*"
# Find regular files
find . -type f -name "*.conf"
Search by size:
# Find files larger than 10MB and smaller than 100MB
find . -size +10M -size -100M
# Find files larger than 100MB
find /var/log -size +100M
Search by time:
# Find files modified in the last 7 days
find /etc -mtime -7
# Find files modified more than 30 days ago
find /tmp -mtime +30
Combine conditions and execute operations:
# Find .conf files and ask if they should be deleted
find /tmp -name "*.conf" -size +1k -ok rm {} \;
# Find all .c files and execute ls -l command
find . -name "*.c" -exec ls -l {} \;
# Search for lines containing "ERROR" in all .log files
find . -name "*.log" -exec grep "ERROR" {} \;
1.4 Source Code
<span>find</span> is part of the GNU findutils toolkit, and the source code can be obtained as follows:
# Find the package that contains the find command
dpkg -S $(which find)
# Download the source package (source repository must be enabled)
sudo apt-get update
apt-get source findutils
Official source address: GNU findutils
(Link available at the bottom of the article)
1.5 References
- System manual:
<span>man find</span>or<span>info find</span> - Official documentation: GNU findutils official documentation
- Chinese manual: Arch Linux find manual
(Link available at the bottom of the article)
2. Detailed Explanation of the grep Command
2.1 Purpose
<span>grep</span> is a classic text search tool used to search for matching strings or regular expressions within file contents. It is feature-rich and supports regular expressions, making it one of the core tools for text processing in Linux.
2.2 Syntax
grep [options] "pattern" [file...]
Common options:
<span>-r</span>or<span>-R</span>: Recursively search directories<span>-n</span>: Display line numbers of matching lines<span>-i</span>: Ignore case<span>-v</span>: Inverse match (show non-matching lines)<span>-l</span>: Only show names of files containing matches<span>-A n</span>: Show matching lines and the following n lines<span>-B n</span>: Show matching lines and the preceding n lines<span>-C n</span>: Show matching lines and n lines before and after
2.3 Examples
Basic search:
# Search for a string in a file
grep "error" /var/log/syslog
# Search in multiple files
grep "main" *.c
Recursive search:
# Recursively search in a directory
grep -rn "main()" /home/user/project/
# Display line numbers
grep -n "TODO" *.py
Ignore case:
grep -i "warning" application.log
Show context:
# Show matching lines and 2 lines before and after
grep -A 2 -B 2 "critical error" logfile.txt
# Or use -C option
grep -C 2 "critical error" logfile.txt
Only list file names:
# Only list file names containing "TODO"
grep -l "TODO" *.js
Inverse match:
# Show all lines not starting with # (filter comments)
grep -v "^#" config.conf
Used with find:
# Find all configuration files containing "port"
find . -name "*.conf" | xargs grep "port"
2.4 Source Code
<span>grep</span> is part of the GNU toolkit, and the source code can be obtained as follows:
# Find the package that contains the grep command
dpkg -S $(which grep)
# Download the source package
apt-get source grep
Official source address: GNU grep
(Link available at the bottom of the article)
2.5 References
- System manual:
<span>man grep</span> - Official documentation: GNU Grep official manual
- Linux man page: man7.org grep
- Chinese manual: Arch Linux grep manual
(Link available at the bottom of the article)
3. Detailed Explanation of the ripgrep (rg) Command
3.1 Purpose
<span>ripgrep</span> (abbreviated as <span>rg</span>) is a high-performance alternative to <span>grep</span>, written in Rust. It defaults to recursive searching, automatically skips hidden and binary files, and is faster than <span>grep</span>, making it very developer-friendly.
3.2 Syntax
rg [options] "pattern" [path...]
Common options:
<span>-n</span>: Display line numbers<span>-i</span>: Ignore case<span>-t <type></span>: Specify file type (e.g., py, js, rs, etc.)<span>-g</span>: Use glob patterns to filter files<span>-C n</span>: Show matching lines and n lines before and after<span>--type-list</span>: View the list of supported file types
3.3 Examples
Basic search:
# Recursively search the current directory and subdirectories
rg "function_"
# Search in a specified directory
rg "function" src/
Specify file type:
# Search only in Python files
rg -t py "import requests"
# Search only in JavaScript files
rg -t js "console.log"
# View the list of supported file types
rg --type-list
Show line numbers and context:
# Show line numbers and 1 line of context
rg -n -C 1 "panic" src/
Exclude directories:
# Ignore the node_modules directory
rg -g "!node_modules/" "console.log"
# Ignore the logs directory
rg "error" -g "!logs/*"
Ignore case:
rg -i "TODO" .
3.4 Source Code
<span>ripgrep</span> is an open-source project, and the source code is hosted on GitHub:
- GitHub repository: BurntSushi/ripgrep
- Installation method:
# Ubuntu/Debian sudo apt install ripgrep # Or install using cargo cargo install ripgrep
3.5 References
- Project homepage: GitHub ripgrep
- Built-in help:
<span>rg --help</span>or<span>man rg</span> - Chinese translated documentation: ripgrep Chinese translation
How to obtain links: Follow the official account and reply with the corresponding command name, such as “find”