In the Ubuntu system, we often need to locate specific files or directories within the file system. Manually searching through each directory is inefficient, so we can utilize the powerful find command. The find command acts like a “file detective,” helping us quickly pinpoint files based on various conditions (such as filename, size, modification time, etc.), making it especially suitable for batch file processing scenarios.
Basic Syntax: How to Use the find Command?¶
The basic format of the find command is straightforward, with the core being “where to search” + “search conditions” + “what to do.” The syntax is:
find [starting path] [search conditions] [actions]
- Starting Path: Specifies the directory to start searching from. The default is the current directory (
.). - Search Conditions: Defined using parameters (e.g.,
-name,-type), such as filename, type, size, etc. - Actions: Performs operations on found files, like deletion (
-delete), display (-print), or executing other commands (-exec).
Common Parameters and Examples: Making find More Precise¶
Here are some of the most commonly used parameters for beginners, each paired with simple examples to help you get started quickly.
1. Search by Filename: -name Parameter¶
The -name parameter matches filenames (or directory names) exactly and supports wildcards (e.g., * for any characters, ? for a single character).
Syntax:
find [path] -name "filename matching rule"
Examples:
- Find all .txt files in the current directory:
find . -name "*.txt"
(. denotes the current directory; *.txt matches all files ending with .txt.)
- Find a file named
READMEsystem-wide (case-insensitive):
find / -iname "README" # / is the root directory; -iname is case-insensitive
2. Search by File Type: -type Parameter¶
Use -type to filter files by their type (e.g., regular files, directories, symlinks). Common types include:
- f: Regular file (not a directory or symlink)
- d: Directory
- l: Symbolic link (soft link)
- c: Character device file (e.g., serial port)
Examples:
- Find all directories in the current directory:
find . -type d
- Find all regular files in the current directory (excluding directories and symlinks):
find . -type f
3. Search by File Size: -size Parameter¶
The -size parameter filters files by size, with common units: k (kilobytes), M (megabytes), G (gigabytes).
- +N: Files larger than N units
- -N: Files smaller than N units
- N: Files exactly N units
Examples:
- Find files larger than 1MB in the current directory:
find . -size +1M
- Find small files (less than 10KB) in the current directory:
find . -size -10k
4. Search by Modification Time: -mtime Parameter¶
The -mtime parameter filters files by their “last modification time,” measured in days.
- -N: Files modified in the last N days
- +N: Files modified more than N days ago
Examples:
- Find files modified in the last 7 days in the /home directory:
find /home -mtime -7 # /home is the starting path; -7 means within 7 days
5. Execute Actions: -exec Parameter¶
The -exec parameter runs a command on the found files (e.g., delete, copy, move). The syntax is:
find [path] [conditions] -exec [command] {} \;
{}: Placeholder for the found files (must use{}and cannot be omitted).\;: Terminator to indicate the end of the command.
Examples:
- Delete all .log log files in the current directory:
find . -name "*.log" -exec rm {} \;
(Always verify the files before deletion to avoid accidental data loss!)
Combining Parameters: Multiple Conditions at Once¶
The find command supports combining multiple conditions, separated by spaces. For example, “Find .txt files modified in the last 7 days and smaller than 100KB in the current directory”:
find . -name "*.txt" -mtime -7 -size -100k
Practical Tips: Avoid Accidental Operations¶
- Confirm before deletion: First list results with
-print, then delete. For example:
find . -name "*.tmp" -print # First print matching files
# After confirmation, use -delete to remove:
find . -name "*.tmp" -delete
- Limit directory depth: To search only the current directory (not subdirectories), use
-maxdepth 1:
find . -maxdepth 1 -type f # Only search regular files in the current directory
Summary¶
The find command is a “Swiss Army knife” for batch file processing in Ubuntu. By flexibly combining parameters, you can easily search by name, type, size, time, etc. The key is to practice simple examples and familiarize yourself with parameter combinations. When using -delete or -exec, always verify the target files to avoid accidental deletion of important data!
(For complex needs like “find and move files” or “count file quantities,” explore advanced parameters like -execdir or -count. However, basic usage covers 90% of daily scenarios.)