Searching for Files by Permission
[If you aren't comfortable with octal numbers and the way UNIX uses them in file permissions, article in is good background reading. -JP]
find can look for files with specific permissions. It uses an octal number for these permissions. The string rw-rw-r--
indicates that you and members of your group have read and write permission, while the world has read-only privilege. The same permissions are expressed as an octal number as 664. To find all *.o files with the above permissions, use:
%find . -name \*.o -perm 664 -print
To see if you have any directories with write permission for everyone, use:
%find . -type d -perm 777 -print
The examples above only match an exact combination of permissions. If you wanted to find all directories with group write permission, you want to match the pattern --w--
. 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
| |
rwxrwxr-x
| |
rw-rw-rw-
| |
rw-rw-r--
| |
rw--rw----
|
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 ().
- BB