5

I read man page for findbut it is not clear for me.

find -perm -mode ------>at least these bit(s) must be set for a file to match

For example: find -perm -754 finds 754,755,757,774,777

What about? find -perm /754 please explain to me by examples

2 Answers 2

6

It's basically the difference between all three bits (-mode) and any single bit (/mode) permission (-perm) subset test.

  • find -perm -mode:

    In this case the permission bits mentioned must be present for the file. For example, if you do find -perm -666 and if a file has 776, it will be matched. Similarly 666, 777 etc will be matched too, but 665 won't be matched. In summary, the mentioned (three) bits must be a subset of the permission bits.

  • find -perm /mode:

    Here any one bit of subset would do. For example, if we do find -perm /666, and if a file has 644, the file will be matched because the user permission bit is 6, and we are looking for a single bit subset. Similarly, 700, 060, 006 etc will be matched, but not e.g. 444, as no bit contains any subset of the required permission bits.

5
  • thank you so much. can we say /mode is useful for find one portion, for example find -perm /u=s?it will find all file with suid?
    – Sinoosh
    Sep 26, 2016 at 7:17
  • 1
    @Sinoosh Not quite, both find -perm /u=s and find -perm -u=s would give you the same result. To spot the difference, add another bit e.g. check find /bin -type f -perm '/u=s,g=s' and find /bin -type f -perm '-u=s,g=s'
    – heemayl
    Sep 26, 2016 at 7:21
  • Sir,only i have another questions ,in find -perm /666 764 will be matched?
    – Sinoosh
    Sep 26, 2016 at 7:27
  • 1
    @Sinoosh Spot on. 764 will be matched for both first and second 6.
    – heemayl
    Sep 26, 2016 at 7:29
  • i got it , -mode is a part of /mode and its result less than /mode
    – Sinoosh
    Sep 26, 2016 at 7:35
1

The other answer correctly explains the find -perm -mode part. However, this answer corrects an incorrect claim about the find -perm /mode part.

Here any one bit of subset would do. For example, if we do find -perm /666, and if a file has 644, the file will be matched because the user permission bit is 6, and we are looking for a single bit subset. Similarly, 700, 060, 006 etc will be matched, but not e.g. 444, as no bit contains any subset of the required permission bits.

The first sentence is true, however, the part claiming 444 would not be matched is not.

-perm /mode matches if any permission bit matches, not a whole number.

When passing /666, we are asking the find command to look for files that have any of the following bits rw-rw-rw (6 = 4+2 which means rw-). So it will match any file that is readable, or writable, or both by any type of owner (user, group or other). That means the only files that will not be matched are files with the following permissions: 000, 001, 010, 011, 100, 101, 110, and 111. Any other permission will be matched by -perm /666 as it would have either the read or write flags assigned to it.


For more clarification, check the following example.

for h in {0..7}; \
do for i in {0..7}; \
do for j in {0..7}; \
do for k in {0..7}; \
do \
touch $h$i$j$k; \
chmod $h$i$j$k $h$i$j$k; \
done; \
done; \
done; \
done 
find -perm /666 -printf '%04m %M\n' | grep 444
7444 -r-Sr-Sr-T
6444 -r-Sr-Sr--
5444 -r-Sr--r-T
4447 -r-Sr--rwx
4446 -r-Sr--rw-
4445 -r-Sr--r-x
4444 -r-Sr--r--
4443 -r-Sr---wx
4442 -r-Sr---w-
4441 -r-Sr----x
4440 -r-Sr-----
3444 -r--r-Sr-T
2444 -r--r-Sr--
1444 -r--r--r-T
0444 -r--r--r--

So the only thing it does not match are the files that have permissions not containing 2 or 4: i.e. all files that contain only 1 or 0:

# remove special permissions (since it was 0 every file that matches and only differs in the set of special permissions is found again)
find \! -perm /666 -printf '%04m %M\n' | grep '^0'| sed 's/ .../ /g'
0111 x--x--x
0110 x--x---
0101 x-----x
0100 x------
0011 ---x--x
0010 ---x---
0001 ------x
0000 -------

maybe to clarify a bit:

given the following permissions

  • WXYZ: file permissions (e.g. 0755 W=0, Z=5)
  • wxyz: search permissions
  • w&W is > 0 if at least one bit matches

/ is an OR operation: return w&W || x&X|| y&Y || z&Z

i.e. return if file contains any permission (W-Z) that matches (at least) the searched permissions (w-z).

while - is an AND operation: return w&W && x&X && y&Y && z&Z

i.e. return if file contains all permissions (W-Z) match (at least) the searched permissions (w-z).

In both cases that can mean that W has set more than w bits (6 sets 2 and 4, but not 3 or 1)

3
  • actually it answers the question in more detail than the others...
    – Petunia
    Jan 27, 2021 at 14:19
  • This answer is correct and touches on an important correction that is made in the other answer. Though it could be edited and salvaged into making a more complete answer.
    – Dan
    Jan 27, 2021 at 14:45
  • I have edited your answer to add more explanation. Feel free to edit it, or revert back. I recommend however you make your example a bit more simple and exclude the special bit as it's not common knowledge for non-advanced users and can make increase confusion a bit.
    – Dan
    Jan 27, 2021 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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