Which command to use to find all files/folders with non-default permissions

filesfindpermissions

Suppose I have a folder, containing other files and folders, and I'd like to find out recursively which subfiles and subfolders have non-default permissions (i.e., not 644 or 755).

Which command can be used to do that? The command should output a list of the relevant files and folders, and their permissions.

Best Answer

You can do the entire task using just find:

$ find . -type f ! \( -perm 755 -o -perm 644 \) -printf "%m\t%p\n"

Example

Make all permutations of permissions (000-777).

$ touch {0..7}{0..7}{0..7}
$ for i in {0..7}{0..7}{0..7}; do chmod $i $i;done
$ find . -type f | wc -l
512

A sample of our find command's list of files it's finding:

$ find . -type f ! \( -perm 755 -o -perm 644 \) -printf "%m\t%p\n"| head -10
734 ./734
376 ./376
555 ./555
663 ./663
256 ./256
336 ./336
2   ./002
152 ./152
527 ./527
416 ./416

If we run our find command we can confirm that it worked:

$ find . -type f ! \( -perm 755 -o -perm 644 \) -printf "%m\t%p\n" | grep 755
$ find . -type f ! \( -perm 755 -o -perm 644 \) -printf "%m\t%p\n" | grep 644
Related Question