find command – list all directories with a set sticky bit

This doesn’t work

find / -type d -perm 1000

The problem is, that it matches only the exact permission (1000), but I only want to find out if the sticky bit is set or not. I don’t care about the other permissions…

My solution so far

I feel that there must be a better way than this:

find / -type d -perm 1001 -or -perm 1002 -or -perm 1003 -or -perm 1004 -or -perm 1005 -or -perm 1006 -or -perm 1007 -or -perm 1010 -or -perm 1011 -or -perm 1012 -or -perm 1013 -or -perm 1014 -or -perm 1015 -or -perm 1016 -or -perm 1017 -or -perm 1020 -or -perm 1021 -or -perm 1022 -or -perm 1023 -or -perm 1024 -or -perm 1025 -or -perm 1026 -or -perm 1027 -or -perm 1030 -or -perm 1031 -or -perm 1032 -or -perm 1033 -or -perm 1034 -or -perm 1035 -or -perm 1036 -or -perm 1037 -or -perm 1040 -or -perm 1041 -or -perm 1042 -or -perm 1043 -or -perm 1044 -or -perm 1045 -or -perm 1046 -or -perm 1047 -or -perm 1050 -or -perm 1051 -or -perm 1052 -or -perm 1053 -or -perm 1054 -or -perm 1055 -or -perm 1056 -or -perm 1057 -or -perm 1060 -or -perm 1061 -or -perm 1062 -or -perm 1063 -or -perm 1064 -or -perm 1065 -or -perm 1066 -or -perm 1067 -or -perm 1070 -or -perm 1071 -or -perm 1072 -or -perm 1073 -or -perm 1074 -or -perm 1075 -or -perm 1076 -or -perm 1077 -or -perm 1101 -or -perm 1102 -or -perm 1103 -or -perm 1104 -or -perm 1105 -or -perm 1106 -or -perm 1107 -or -perm 1110 -or -perm 1111 -or -perm 1112 -or -perm 1113 -or -perm 1114 -or -perm 1115 -or -perm 1116 -or -perm 1117 -or -perm 1120 -or -perm 1121 -or -perm 1122 -or -perm 1123 -or -perm 1124 -or -perm 1125 -or -perm 1126 -or -perm 1127 -or -perm 1130 -or -perm 1131 -or -perm 1132 -or -perm 1133 -or -perm 1134 -or -perm 1135 -or -perm 1136 -or -perm 1137 -or -perm 1140 -or -perm 1141 -or -perm 1142 -or -perm 1143 -or -perm 1144 -or -perm 1145 -or -perm 1146 -or -perm 1247 -or -perm 1350 -or -perm 1351 -or -perm 1352 -or -perm 1453 -or -perm 1454 -or -perm 1555 -or -perm 1556 -or -perm 1657 -or -perm 1660 -or -perm 1661 -or -perm 1662 -or -perm 1663 -or -perm 1764 -or -perm 1765 -or -perm 1766 -or -perm 1767 -or -perm 1770 -or -perm 1771 -or -perm 1772 -or -perm 1773 -or -perm 1774 -or -perm 1775 -or -perm 1776 -or -perm 1777
Asked By: Pixelbog

||

Since you are interested in only one specific mode bit, you should be able to use either the -perm -MODE or -perm /MODE syntax. From the man page:

-perm -mode

All of the permission bits mode are set for the file. Symbolic modes are accepted in this form, and this is usually the way in which you would want to use them. You must specify u, g or o if you use a symbolic mode. See the EXAMPLES section for some illustrative examples.

-perm /mode

Any of the permission bits mode are set for the file. Symbolic modes are accepted in this form. You must specify u, g or o if you use a symbolic mode. See the EXAMPLES section for some illustrative examples. If no permission bits in mode are set, this test matches any file (the idea here is to be consistent with the behaviour of -perm -000).

As a usage example, I created two directories and set the sticky bit on one:

test$ mkdir sticky-bit-test

sticky-bit-test$ mkdir dir-with-sticky
sticky-bit-test$ chmod +t dir-with-sticky/
sticky-bit-test$ mkdir dir-without-sticky
sticky-bit-test$ ls -l
total 8
drwxr-xr-x 2 user group 4096 Jul 19 18:55 dir-without-sticky
drwxr-xr-t 2 user group 4096 Jul 19 18:55 dir-with-sticky

test$ find . -type d -perm -1000
./sticky-bit-test/dir-with-sticky

test$ find . -type d -perm /1000
./sticky-bit-test/dir-with-sticky

As you see, both options correctly single out the directory with sticky bit set. As noted by @ilkkachu, the distinction between these two is further elaborated in this question.

Answered By: AdminBee

Disclaimer: I’m the current author of the rawhide (rh) program used at the end of this answer.

Note that the /1000 notation works on GNU find, but not on macOS (and other systems probably). There you need to change that to +1000:

find / -type d -perm +1000

With rh, you can do this:

rh / 'd && sticky'

The rh command can be found here: github.com/raforg/rawhide

Answered By: raf
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.