find files which have a higher permission than xxx

16,165

Solution 1

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

Solution 2

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:
16,165
Chef Flambe
Author by

Chef Flambe

Updated on September 18, 2022

Comments

  • Chef Flambe
    Chef Flambe almost 2 years

    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.

    • Admin
      Admin over 12 years
      Can you clarify "higher"? How would rw-r-x--- compare to rwxr-----?
    • Admin
      Admin over 12 years
      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).
    • Admin
      Admin over 12 years
      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
    • Admin
      Admin over 2 years
      Question: is 444 higher than 640? What about 500? Are you actually asking for files that have permission bits other than 640?
  • Chef Flambe
    Chef Flambe over 12 years
    There were a comment which proposed find . -type f -perm /137. Do you know what was wrong with that?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 12 years
    @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.
  • Chef Flambe
    Chef Flambe over 12 years
    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.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 12 years
    @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.