3

In the following example directory

$ ls -l
total 0
-rw-r--r--  1 user  testgroup  0 12 feb 12:00 file1
-rw-rw-r--  1 user  testgroup  0 12 feb 12:00 file2
-rw--w-r--  1 user  testgroup  0 12 feb 12:00 file3
-rw-r--r--  1 user  testgroup  0 12 feb 12:00 file4
-rw-rwxr--  1 user  testgroup  0 12 feb 12:00 file5

I would like to find all the files where group permissions are exactly -w-, that is 2 (only write permission).

I am using

$ bash --version
GNU bash, versione 4.4.23(1)-release (amd64-portbld-freebsd12.0)

on FreeBSD 12. It is not GNU find.

My attempt was with

$ find . -perm -g+w
./file2
./file3
./file5

but this returns all the files having at least group write permissions; I would like to list instead the files whose group is only permitted to write. How to accomplish this?

2
  • have you tried g=w? Feb 12, 2019 at 12:13
  • @ctrl-alt-delor Yes, the result is empty. No files found.
    – BowPark
    Feb 12, 2019 at 12:23

1 Answer 1

7

You can add more conditions to exclude files with other permission bits set.

find . -perm -g+w ! -perm -g+r ! -perm -g+x

or (as proposed in steeldriver's comment)

find . -perm -g+w ! -perm /g+rx

Example:

$ ls -l file*
-rw-r--r-- 1 bodo bodo 0 Feb 12 12:50 file1
-rw-rw-r-- 1 bodo bodo 0 Feb 12 12:50 file2
-rw--w-r-- 1 bodo bodo 0 Feb 12 12:50 file3
-rw--wxr-- 1 bodo bodo 0 Feb 12 12:50 file4
-rw-rwxr-- 1 bodo bodo 0 Feb 12 12:50 file5
$ find . -perm -g+w ! -perm -g+r ! -perm -g+x
./file3
$
$ find . -perm -g+w ! -perm /g+rx
./file3
$
2
  • +1, it works! It can surely be used this way. But I wonder if there is a direct way: it would be weird if there wasn't.
    – BowPark
    Feb 12, 2019 at 12:27
  • You can make it slightly more compact in GNU find using the /perm form e.g. -perm -g+w ! -perm /g+rx Feb 12, 2019 at 13:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .