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.

I want to find all files which have a higher permission than e.g. 640. Perhaps this would work with a find and exec command. But my knowledge isn't sufficient for such a task.

share|improve this question
    
Can you clarify "higher"? How would rw-r-x--- compare to rwxr-----? –  Ulrich Schwarz Feb 15 '12 at 16:50
    
Good question. Your first example would be 650, your second 740. But both are higher than the number 640. So both should be appear in the results. Alternatively, it would be nice to search for files with specific permissions (like 755 or 777). –  testing Feb 15 '12 at 17:23
1  
You can use find with the -perm switch to search for files with certain permissions: e.g. find some_dir/ -type f -perm 666 will list all files with -rw-rw-rw- permissions. You can also use symbolic perms .. -perm ugo=rw –  Herman Torjussen Feb 15 '12 at 17:32

3 Answers 3

up vote 4 down vote accepted

I think what you're after is

find -perm -640 ! -perm 640

i.e search files that have at least all the permissions in 640 and that don't have 640 as the permission bits. Or, in other words, amongst the files that are readable and writable by their owner and readable by the group, search files that are executable or writable by someone other than the owner or world-readable (assuming no ACLs). You may want to add -type f to restrict to regular files, or at least ! -type d -o -type d -perm 750 ! -perm 750 to allow directories to have execution permission.

If you want to match files whose permission bits, interpreted as an integer, are higher than 0o640 (which doesn't really make any sense), you're going to have to enumerate several cases. If you look at the bitwise representation, there are two ways for a number between 0 and 0o777 to be larger than 0o640: either the 0o100 bit is set in addition to the 0o600 bits, or the 0o640 bits are set. Remove the final ! -perm 640 if you want permissions 0o640 to match.

find -perm -700 -o -perm -640 ! -perm 640
share|improve this answer
    
There were a comment which proposed find . -type f -perm /137. Do you know what was wrong with that? –  testing Feb 21 '12 at 10:49
    
@testing -perm /137 searches files that have at least one of the specified bits set, i.e. executable by anyone or writable by anyone but the owner or world-readable. It may in fact be what you're after, but it's not what you asked. –  Gilles Feb 21 '12 at 13:05
    
Thanks for your answer. What does the -o mean? Or? and ! means NOT? Does ! -type d -o -type d -perm 750 ! -perm 750 mean to find no directories or directories with at least 750 permission with not 750 permission bits? find -perm -700 -o -perm -640 ! -perm 640 shows me many directories ... Does this command mean to find files with 700 permission or at least 640 permission with no 640 permission bits set? I'm trying to get this notation under my hood. –  testing Feb 21 '12 at 19:25
1  
@testing -o is or, ! is not, and juxtaposition is and. Also, test1 -o test2 test3 means test1 or (test2 and test3). -perm -700 -o -perm -640 ! -perm 640 finds files that either have all the bits in 700 set, or have all the bits in 640 set and at least one more. –  Gilles Feb 21 '12 at 19:27

Here is great command that you can edit for your own use:

find -perm -o+r -exec stat --printf='%A %a %n --- %F\n' {} \;

Example of result:

-rw-r--r-- 644 dir1//file4 --- regular file
lrwxrwxrwx 777 dir1/file5 --- symbolic link

Finds files with permission for others to read. Then print the permissions in symbolic and octal form, file path and file type.

share|improve this answer

Here is a one-liner you could use:

find . -type f -printf %p:%m\\n | while read x; do name=${x%:*}; perm=${x#*:}; if [[ $perm -gt 640 ]]; then echo $name; fi done

This command finds all the regular files from your current path and prints their name (%p) and their permissions (%m) separated by a colon. Each line of output is further read into a variable $x, which is then split apart into the variables $name and $perm:

${x%:*} means return the value of x, with its suffix removed

${x#:*} means return the value of x, with its prefix removed

If the permissions of the file matches the criteria, print the file name.

PS: For simple use, you can assign the command to an alias:

alias advfind='find ...'
share|improve this answer

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.