
The find command is a cornerstone of any Linux user's toolkit. It allows you to locate files and directories with pinpoint accuracy based on a variety of criteria. While the basic functionality is straightforward, the power lies in its advanced options.
This blog delves into these advanced features, equipping you to navigate the complex file systems of your Linux machine with ease.
Beyond the Basics:
We've all used find /path/to/search -name "filename" to locate specific files. But what if you need to find files based on size, modification time, or permissions? Here's where the magic begins:
Size Matters:
-size +20M: Find files larger than 20 Megabytes.-size -40k: Find files smaller than 40 Kilobytes.-size 60c: Find files exactly 60 bytes in size.
Time Travel with Modification Dates:
-mtime -7: Find files modified within the last 7 days.-atime +30: Find files accessed in the past 30 days.-ctime +1w: Find files where the inode (file metadata) changed last week (w stands for weeks).
Permission Precision:
-perm 755: Find files with read, write, and execute permissions for owner, read and execute for group, and no permissions for others.-u john -perm 640: Find files owned by user "john" with read/write permissions for the owner only.
Combining the Powerhouse:
The true strength of find lies in combining these options for highly specific searches. For example:
find /var/log -user bijaydas -mtime -2 -name "auth.log*"
This command searches the /var/log directory for log files (auth.log) owned by the user "bijaydas" that were modified in the last two days.
Delving Deeper
Searching by Time
You can search for files based on when they were last accessed (atime), modified (mtime), or changed (ctime).
Files modified in the last 7 days:
find . -mtime -7
Files accessed more than 30 days ago:
find . -atime +30
Using -exec for Command Execution
The -exec option allows you to execute a command on each file found.
Delete all
.logfiles:
find . -name "*.log" -exec rm {} \;
Change permissions of all
.shfiles:
find . -name "*.sh" -exec chmod +x {} \;
Combining with grep for Content Search
You can combine find with grep to search for files containing specific text.
Find
.txtfiles containing "auth":
find . -name "*.txt" -exec grep -l "auth" {} \;
Conclusion
The find command is a versatile and powerful tool in the Linux command line arsenal. You can perform complex searches and operations efficiently by mastering its advanced options. Whether you are a system administrator, developer, or just a power user, find can help you streamline your workflow and manage your files more effectively.
By practicing with the examples provided in this guide, you'll gain a deeper understanding of how to leverage the full potential of the find command.
To learn more about find command visit man7.org
Happy searching!
