The find command in Linux system is a powerful tool for searching for files in the file system and performing corresponding operations. Whether you are a system administrator or an ordinary user, mastering the find command can greatly improve work efficiency. This article will introduce the usage of the find command in detail and demonstrate its practical application through multiple examples.
Basic syntax
The basic syntax of the find command is as follows:
find [path] [Options] [operate]
Among them, the path parameter specifies the starting directory of the search; the options are used to set the search conditions, such as file name, file type, permissions, etc.; the operations are specific actions performed on files that meet the conditions, such as printing the file path, executing commands, etc.
Common options
Search by file name
-name:Search by file name,Support wildcard characters。 -iname:and -name similar,But ignore the case of filename。
Example:
# Find all files named "" in the current directoryfind . -name ""
Find all files ending with ".txt" in the current directory
find . -name "*.txt"
Find all files ending with ".txt" in the current directory, ignoring upper and lower case
find . -iname "*.txt"
Find by file type
-type: Look up by file type, optional types include f (normal file), d (directory), l (symbol link), etc.
Example:
# Find all directories in the current directoryfind . -type d
Find all normal files in the current directory
find . -type f
Search by file permissions
-perm: Search by file permissions, you can use octal or symbolic notation.
Example:
# Find all files with permissions of 755 in the current directoryfind . -perm 755
Find all files in the current directory that have write permissions to users in the same group
find . -perm /g=w
Find by file size
-size:Find by file size,Available +(Greater than)、-(Less than)and =(equal)Specify the size,Units include b(byte)、c(piece)、k(千byte)、M(兆byte)wait。
Example:
# Find all files larger than 100MB in the current directoryfind . -size +100M
Find all files less than 1KB in the current directory
find . -size -1k
Search by file modification time
-mtime:Search by file modification time,use -n(n Within the day)、+n(n Day ago)and n(exactly n Day ago)Specify the time range。 -atime:Search by file access time。 -ctime:Search by file status change time。
Example:
# Find all files modified within 7 days in the current directoryfind . -mtime -7
Find all files in the current directory that have not been accessed for more than 30 days
find . -atime +30
Practical Examples
Find and delete files of the specified type
Assuming that you need to delete all files with the .log extension in the system, you can use the following command:
find / -type f -name "*.log" -exec rm {} \;
Note: In actual use, please make sure you have the corresponding permissions and use the -exec option with caution to avoid accidentally deleting important files.
Find and display file details
If you need to find a specific file and display its details, you can use the -exec option combined with the ls -l command:
find /home -type f -name "" -exec ls -l {} \;
Find and execute commands
For the found files, you can execute any command. For example, calculate the MD5 checksum of all files named "":
find . -type f -name "" -exec md5sum {} \;
Find empty files
Find all empty files in the current directory and its subdirectories:
find . -type f -empty
Find large files
Find all files larger than 1GB in the current directory and its subdirectories:
find . -type f -size +1G
Summarize
find
Commands are a very powerful file search tool in Linux systems. By using various options and operations rationally, we can efficiently manage and maintain the file system. Mastering the usage of the find command is of great significance to improving Linux operation efficiency.
This is the end of this article about the detailed explanation and practical examples of Linux find commands. For more related contents of Linux find commands, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!