In the world of Linux, file management is a core task. Whether system administrators are maintaining servers or developers are searching for specific code files, quickly and accurately locating files is crucial.
1. Basic Syntax of the ‘find’ Command
The basic syntax of the ‘find’ command is:
find [search path] [expression]
Search path: Specifies the directory from which to start the search. If omitted, the default is the current directory (.). For example, to search in the root directory, use “/”; to search in the user’s home directory, use “$HOME”.
Expression: Used to define search criteria, which is a combination of options and test conditions that can filter files based on various attributes such as name, size, modification time, permissions, etc.
2. Searching by File Name
2.1 Exact Match by File Name
To find a file named “example.txt”, you can use the following command:
find -name “example.txt”
This command will search for files named “example.txt” in the current directory and its subdirectories, outputting their paths.
2.2 Using Wildcards for Search
Wildcards are extremely common in file name searches:
“*” matches any sequence of characters (including an empty sequence). For example, to find all files ending with “.log”:
find -name “*.log”
“?” matches any single character. For instance, to find files with three characters that start with “t”:
find -name “t??”
“[…]” matches any one of the characters within the brackets. For example, to find files where the second character is “a”, “e”, or “i”:
find -name “?[aei]*”
2.3 Case-Insensitive Search
When the case of the file name is not important, you can use the “-iname” option. For example, to find a file named “ReadMe.txt” without case sensitivity:
find -iname “ReadMe.txt”
This command can find files like “ReadMe.txt”, “readme.txt”, “README.TXT”, etc.
3. Searching by File Path
If you know part of the file’s path, you can search by path using the “-path” option. For example, to find all “.pdf” files in the “/home/user/Documents” directory:
find /home/user/Documents -name “*.pdf”
If you want to ignore case in the path, you can use the “-ipath” option.
4. Searching by Time
The Linux system records three types of time attributes for each file:
Access time (atime): The last time the file was read.
Modification time (mtime): The last time the file content was modified.
Change time (ctime): The last time the file metadata (such as permissions, owner, etc.) was changed.
4.1 Searching by Modification Time
“-mtime n”: Find files modified n days ago, where a positive number indicates n days ago, and a negative number indicates within n days. For example, to find files modified 2 days ago:
find -mtime 2
“-mtime +n”: Find files modified more than n days ago (excluding those modified within n days). For example, to find files modified 3 days ago or earlier:
find -mtime +3
“-mtime -n”: Find files modified within the last n days. For example, to find files modified in the last day:
find -mtime -1
To search by minutes, you can use the “-mmin” option. For example, to find files modified in the last 30 minutes:
find -mmin -30
4.2 Searching by Access Time or Change Time
To search by access time, use the “-atime” option, which works similarly to “-mtime”. For example, to find files accessed in the last 4 days:
find -atime -4
To search by change time, use the “-ctime” option. For example, to find files whose status changed 5 days ago:
find -ctime 5
There are also corresponding “-amin” and “-cmin” options for minute-level time searches.
5. Searching by File Size
The “-size” option is used to search for files by size. File sizes can be specified with units:
“c” represents bytes.
“w” represents double bytes.
“b” represents 512-byte blocks (this is the default unit; if no unit is specified, it defaults to this calculation).
“k” represents kilobytes.
“M” represents megabytes.
“G” represents gigabytes.
5.1 Finding Files Larger or Smaller than a Specified Size
To find files larger than 10MB:
find -size +10M
To find files smaller than 500KB:
find -size -500k
5.2 Finding Files of a Specific Size
To find files that are exactly 1GB in size:
find -size 1G
5.3 Finding Files within a Size Range
To find files that are between 10MB and 50MB in size:
find -size +10M -size -50M
6. Searching by File Type
The “-type” option is used to specify the file type:
“d” represents directories. For example, to find all subdirectories in the current directory:
find -type d
“f” represents regular files. For example, to find all “.sh” script files:
find -type f -name “*.sh”
“l” represents symbolic links. For example, to find all symbolic link files:
find -type l
“c” represents character device files.
“b” represents block device files.
“p” represents named pipe files.
“s” represents socket files.
7. Searching by File Permissions
The “-perm” option is used to search for files based on permissions. Permissions can be represented in octal or symbolic notation.
7.1 Exact Permission Match
To find files with permissions of “644”:
find -perm 644
7.2 Searching by Owner, Group, or Other Permissions
To find files where the owner has execute permissions:
find -perm /u=x
To find files where the group has read and write permissions:
find -perm /g=rw
To find files where others have read permissions:
find -perm /o=r
7.3 Finding Files with Specific Permission Conditions
To find files where at least one user (owner, group, or others) has write permissions:
find -perm /w
To find files where permissions satisfy that the owner has read permissions, the group has read permissions, and others have read permissions:
find -perm -u=r,g=r,o=r
8. Combining Conditions for Search
The “find” command allows combining multiple conditions for searching. For example, to find “.log” files larger than 10MB and modified in the last 2 days:
find -name “*.log” -size +10M -mtime -2
To find files owned by user “user1” with permissions of “755”:
find -user user1 -perm 755
9. Executing Operations on Found Files
9.1 Using the “-exec” Option
The “-exec” option allows executing a specified command on each found file. The syntax is:
find [search path] [expression] -exec command {} \;
“{}” is a placeholder that will be replaced by the found file name. For example, to find all “.log” files and delete them:
find -name “*.log” -exec rm -f {} \;
To find all “.txt” files and copy their contents to the “backup” directory (assuming the “backup” directory already exists):
find -name “*.txt” -exec cp {} backup/ \;
9.2 Using the “-ok” Option
The “-ok” option is similar to “-exec”, but prompts the user for confirmation before executing the command. For example, to find all files larger than 100MB and delete them using the “-ok” option:
find -size +100M -ok rm -f {} \;
The system will prompt whether to delete each qualifying file, and the user must input “y” or “Y” to execute the delete operation.
9.3 Using the “-delete” Option
The “-delete” option can directly delete found files without an additional “-exec” command. For example, to delete all empty files:
find -empty -delete
This option should be used with caution to avoid accidentally deleting important files.
10. Other Useful Options
10.1 “-maxdepth” and “-mindepth”
“-maxdepth n”: Limits the maximum depth of the search to n levels of directories. For example, to find files in the current directory and its next level subdirectories:
find -maxdepth 1 -name “*.txt”
“-mindepth n”: Specifies to start searching from the n-th level of directories. For example, to find files starting from the second level of subdirectories under the current directory:
find -mindepth 2 -name “*.sh”
10.2 The “-not” Option
The “-not” option is used to negate conditions. For example, to find all files that are not “.jpg”:
find -not -name “*.jpg”
To find files that do not belong to user “user2”:
find -not -user user2
10.3 The “-newer” Option
The “-newer” option is used to find files that are newer than a specified file. For example, to find files newer than “file1.txt”:
find -newer file1.txt
To find files that are newer than any file in the “dir1” directory:
find -newer dir1
The Linux “find” command is extremely powerful, allowing for complex file search requirements through the flexible combination of various options and conditions. From simple file name searches to precise filtering based on time, size, permissions, and more, as well as executing operations on found files, the “find” command plays a key role in file management. I hope this introduction and examples help you master the “find” command and improve your efficiency in operating the Linux system.