Wednesday, January 23, 2008

Searching Files by Permission in Linux

Find can look for files with a specific permission. It uses an octal number
for these permissions. The string rw-rw-r--, indicates you and members of
your group have read and write permission, while the world has read only
priviledge. The same permissions, when expressed as an octal number, is 664.
To find all "*.o" files with the above permission, use:

find . -name *.o -perm 664 -print

If you want to see if you have any directories with world write permission,
use:

find . -type d -perm 777 -print

This only matches the exact combination of permissions. If you wanted to find
all directories with group write permission, there are several combinations
that can match. You could list each combination, but find allows you to
specify a pattern that can be bit-wise ANDed with the permissions of the
file. Simply put a minus sign before the octal value. The group write
permission bit is octal 20, so the following negative value:

find . -perm -20 -print

will match the following common permissions:

+-------------------------+
|Permission Octal value |
+-------------------------+
|rwxrwxrwx 777 |
|rwxrwxr-x 775 |
|rw-rw-rw- 666 |
|rw-rw-r-- 664 |
|rw-rw---- 660 |
+-------------------------+

If you wanted to look for files that you can execute, (i.e. shell scripts or
programs), you want to match the pattern "--x------," by typing:

find . -perm -100 -print

When the -perm argument has a minus sign, all of the permission bits are
examined, including the set user ID bits.

No comments: