3

I'm trying to wrap my head around this command:

find /home/ -type d \( -perm -0002 -a ! -perm -1000 \) 2>/dev/null

I understand that it's going to look in the 'home' directory for all directories and redirect all stderr messages to a file (to suppress them), however I'm confused by the part in the middle, specifically:

\( -perm -0002 -a ! -perm -1000 \)

What do the slashes mean? I'm assuming ! -perm -1000 means to look for directories without those permissions, and -perm -0002 means to look for directories with those permissions, but I'm only used to seeing permissions in the form 644 (for example). Why are there four digits? Also, what does the -a flag do?

4

In the manual you can find:

Operators
              Operators join together the other items within the expression.
              They include for example -o (meaning logical OR) and -a
              (meaning logical AND).  Where an operator is missing, -a is
              assumed.

So literally \( -perm -0002 -a ! -perm -1000 \) means : perm = 0002 and not perm = 1000

perm = 0002 means writeable for Others
perm = 1000 means sticky bit is set.

So this expression search for file writable for Other and without sticky bit sets.

Looking for directory is done with -type d d means directory so find look after object of type directory

From the find manual, -perm has several forms:

  • -perm 0002 will match all files with this exact permission setting
  • -perm -0002 will match all files where this right bit is setup, (which means 0772, 1752... whatever combination if the file is writeable for others)
| improve this answer | |
5

In -type d \( -perm -0002 -a ! -perm -1000 \), first, the slashes escape the parenthesis for the shell, so they can be passed as an argument, instead of being interpreted as special operators by the shell. (You'd need the same for e.g. >, for obvious reasons.) They could be written as '(' or "(", too, but eventually find only sees ( and ).

The parenthesis group the conditions, and -a is an and conjunction. That's also the implicit default for back-to-back conditions, like used here with -type d and the expression in parenthesis, there's no explicit -a connecting them. The expression is exactly equivalent to -type d -perm -0002 ! -perm -1000. Parenthesis are really only needed in with the or conjunction -o.

-type d is obvious, and -perm -NNNN means to look for a file with all of the permission bits NNNN set. Or, in this case, directories with the write for others permission (0002) set, and the sticky bit (1000) unset (because of the negation). I think the combination here could also be written as -perm -o+w ! -perm -o+t. Note the dash in front of the permission string, without it, the meaning would be different. See e.g. the Linux man page for find, look for "( expr )" and "-perm mode".

Write access to a directory allows creating, removing and renaming files inside it, and the sticky bit prevents removing and renaming files owned by other users. The whole expression looks for directories where anyone can remove or rename any files.

| improve this answer | |
2
  • -a: is Logical AND (-o can be used for OR)
  • \(, \): because parentheses has special meaning in shell, the slash used to escape them
  • four-digit permissions are related to sticky bit, setuid, setgid
| improve this answer | |

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.