Finding Files Based on Attributes in Linux

The find command is one of the most powerful tools in Linux, allowing users to search for files and directories based on various attributes such as permissions, size, modification time, and ownership. This guide provides practical examples of how to use find to locate files efficiently.


1. Find Files by Permission

Find Files with Full Permissions (rwxrwxrwx)

find /var -type f -perm 0777        

Finds all files in /var with full read (r), write (w), and execute (x) permissions for owner, group, and others.

Understanding 0777

  • 0 → Specifies an absolute mode (exact match).
  • 777 → Represents rwxrwxrwx (read, write, execute for everyone).

Security Warning:

Files with 0777 permissions pose a security risk as any user can modify or execute them. Consider using chmod to restrict permissions if necessary.


2. Find Files by Size

Find Files Between 5MB and 10MB

find /usr -type f -size +5M -size -10M        

Finds files in /usr that are larger than 5MB but smaller than 10MB.

Understanding -size Option

  • +5M → Files larger than 5MB.
  • -10M → Files smaller than 10MB.
  • Size units:


3. Find Recently Modified Files

Find Files Modified in the Last 30 Minutes

find /usr -type f -mmin -30        

Lists files in /usr modified in the last 30 minutes.

Other Time-Based Options:

  • -mmin -10 → Modified in the last 10 minutes.
  • -mmin +60 → Modified more than 60 minutes ago.
  • -mtime -1 → Modified within the last 24 hours.
  • -atime -7 → Accessed within the last 7 days.


4. Find Files Writable by Group but Not Readable by Others

If you need to find files that have group write (w) permission but prevent others (o) from reading or writing, use:

find /var/log -perm -020 -not -perm /007        

Finds files in /var/log that the group can write to, but others cannot read or write.

Understanding -perm Options:

  • -020 → Ensures the group has write (w) permission.
  • -not -perm /007 → Excludes files that others (o) have read (r) or write (w) permissions.


5. Additional Advanced find Commands

Find and Delete Large Files (Above 1GB)

find /home -type f -size +1G -exec rm -i {} \;        

Finds and interactively deletes (-i for confirmation) files larger than 1GB in /home.

Find Files Owned by a Specific User

find /var/www -type f -user username        

Lists files in /var/www owned by username.

Find Empty Files

find /tmp -type f -empty        

Finds all empty files in /tmp.


Summary of Common find Commands


Article content

Conclusion

The find command is a powerful tool for searching files based on permissions, size, modification time, and ownership.

By understanding and using these commands effectively helps in file management, security auditing, and system maintenance.

By mastering these techniques, you can quickly locate specific files and take necessary actions efficiently. Let us know in the comments which find command you use the most!



To view or add a comment, sign in

More articles by Hajra Umer

Explore content categories