Hello everyone! When using a Linux system, you often find yourself needing to locate specific files among countless directories and files. Are you repeatedly typing “ls” wondering, “Where did I put that?” π«
Today, we’re going to delve into the powerful find command, a tool that pinpoints the files you want as if by magic. Read this article to the end, and you too can become a file search master! π

1. Grasping the Basic Structure of the find Command
The find command isn’t just for searching by name. It can locate files that meet specific criteria and even perform actions on those found files. Its basic structure is as follows:
find [κ²μ μμ κ²½λ‘] [ννμ(쑰건)] [μ‘μ
]
- πΊοΈ Search Start Path: The starting point that tells find where to begin searching. (e.g., /, . current directory, ~/ home directory)
- π Expression: This is where you define the conditions for the files you want to find. (Most important!)
- βοΈ Action: This determines what to do with the found files. (By default, it prints the path to the screen)
Now, let’s explore the world of find, focusing on its most powerful feature: ‘expressions’!
2. π΅οΈββοΈ Conditional Search (1): Finding Files by Permission
When managing system security or files, there are many instances where you need to find files with specific permissions. The -perm option is incredibly useful for this.
Octal Mode and Symbolic Mode
There are two ways to express permissions:
- Octal Mode: Permissions are specified using numbers, like 755 or 644.
- Symbolic Mode: Permissions are specified by combining user (u), group (g), other (o) with permissions (r, w, x), like u+x or g-w.
The Core of Permission Search: Prefixes / and –
The -perm option allows you to change the search method by adding special prefixes before the numbers or symbols.
- find . -perm 755 π Finds files with permissions that are exactly 755. (Less commonly used than you might think)
- find . -perm -644 π Finds files where the permission bits are at least 644. This means it finds files that include all the permissions: rw for the owner, r for the group, and r for other users. (e.g., rwx-r-xr-x, or 755, would also be included here.)
- find . -perm /u+x π Finds all files where the owner (user) has at least some execute (x) permission. It ignores other permissions and only checks the u+x condition. This method is most useful for filtering files with specific permissions! π
- Example: Search for files with owner execute permission under the /opt directory
`
find /opt -type f -perm /u+x
`
####
Bonus: Finding Special Permission (SUID/SGID) Files
SUID (4000) and SGID (2000) are very important permissions for security. It’s essential to find and manage files with these permissions.
- Find SUID-set files across the entire system
`
find / -type f -perm /4000
`
3. βοΈ Conditional Search (2): Finding Files by Size
Sometimes log files grow too large, or unnecessary large files occupy disk space. The -size option allows you to search by size.
- Use + (greater than), – (less than) symbols combined with units.
- Units: c(bytes), k(Kilobytes), M(Megabytes), G(Gigabytes)
| Option | Meaning | Example |
| -size 10M | Exactly 10MB files | πΎ |
| -size +100M | Files larger than 100MB (finding large files) | π |
| -size -1k | Files smaller than 1KB (finding empty or very small files) | π |
– Example: Find large files over 1GB in the home directory
`
find ~/ -type f -size +1G
`
4. βοΈ Execute After Search: Utilizing Powerful Action Features
The true power of find comes from its ‘action’ feature, which links search results with other commands.
Find and Delete: -delete
Use this when you want to find and immediately delete files that meet specific criteria. It’s much safer and simpler than piping to xargs or rm.
- Example: Find and delete all temporary files with the .tmp extension
`
find . -type f -name “*.tmp” -delete
`
Find and Execute Command: -exec
This is the crown jewel of find! πΈ It allows us to execute any desired command for each file found.
Basic Format: -exec [command to execute] {} ;
- {}: A placeholder where the found file name will be inserted.
- ;: Marks the end of the command. (Be careful with the backslash before the semicolon!)
- Example 1: Change permissions to 640 for all log files larger than 10MB
`
find /var/log -type f -name “*.log” -size +10M -exec chmod 640 {} ;
`
- Example 2: Add execute permission to all .sh files in the current directory
`
find . -type f -name “*.sh” -exec chmod +x {} ;
`
###
Conclusion
Today, we explored how to use the find command to search for files by permission and size, and how to delete or execute other commands on the found files. While find has a wide variety of options and might seem complex at first, mastering -perm, -size, -exec, and -delete alone will upgrade your Linux file management skills by several levels.
Stop wasting time searching for files! Become a smart Linux expert by utilizing the find command. π
Tags:
Linux, find, command, file search, permissions, chmod, exec, delete, SUID, system administration, CLI
Leave a Reply