Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 8 down vote accepted

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
share|improve this answer
    
permissions are on 12 bits, not 9. –  Stéphane Chazelas Feb 8 at 15:23
    
@StephaneChazelas - I know I was just showing the common ones. –  slm Feb 8 at 15:33
1  
For robustness, I'd write 0755 etc (it is octal, some idiotic tool might understand decimal...) –  vonbrand Feb 8 at 16:15
1  
@vonbrand, if a find implementation treated -perm 755 as -perm 01363, or a chmod implementation treated chmod 755 as chmod 01362, you'd think someone would have noticed by now and reported it as a bug. Note that there is only one implementation of find that supports -printf. –  Stéphane Chazelas Feb 8 at 17:35
    
@StephaneChazelas, perhaps not find(1), but elsewhere... I got into the habit of making octal for file permissions explicit when I got bitten by decimal (decades ago, don't remember much details). –  vonbrand Feb 8 at 20:38
find . \( \
  -type d ! -perm 755 -o \
  -type l ! -perm 777 -o \
  -type f ! \( -perm 644 -o -perm 755 \) -o \
  ! -type f ! -type l ! -type d ! -perm 644 \) -exec ls -ld {} +
share|improve this answer
find . -printf "%m\t%p\n" | grep -Ev "^(755|644)"

Example output:

$ find . -printf "%m\t%p\n" | grep -Ev "^(755|644)"
777 ./a/writable/folder
777 ./another/folder
666 ./an/executable-file

Please note that the egrep command does not consider whether a line is a file or folder. As a result, files with permission 755 and folders with permission 644 would not be found by this command.

Furthermore this command is not very efficient, as find starts by printing all subfolders and subfiles, and only after that the files and folders are filtered by egrep. However, I haven't found a better method yet.

share|improve this answer
1  
That would miss files like -r-sr-s--T (7550). egrep and find concurrently, there's no efficiency issue here. –  Stéphane Chazelas Feb 8 at 14:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.