How to Find Files Owned by a Group in Linux

Updated

Bash shell script in a Linux terminal

find /path -group GROUPNAME -print. If the group has been deleted and the name no longer resolves, pass the number instead — find /path -group 1004 — and use find /path -nogroup to sweep up files whose GID resolves to nothing. Everything below was executed with BSD find on macOS 26.6; GNU-only behaviour is quoted from GNU find(1) and was not run on Linux. A script, find-files-owned-by-group.sh, builds a scratch tree of its own and re-checks the deleted-group, numeric-GID, -perm, parenthesis, -exec chmod, -delete and exit-status behaviour described below, removing the tree when it exits.

A deleted group makes find fail loudly, not silently

When the group has been deleted, the symptom is unmistakable — find refuses to start rather than returning an empty list:

$ find proj -group developers -type f
find: -group: developers: no such group
rc=1

Contrary to advice you will see everywhere, you do not need -gid to search by number. -group takes a numeric GID on both implementations. BSD find(1): “If gname is numeric and there is no such group name, then gname is treated as a group ID.” GNU: “File belongs to group gname (numeric group ID allowed).” All three of these returned the same three files:

$ find proj -type f -group wheel
$ find proj -type f -group 0
$ find proj -type f -gid 0
proj/dir with spaces/file name.txt
proj/src/app.c
proj/src/lib.c

-gid earns its place for a different reason: on GNU it is a numeric comparison — “File’s numeric group ID is less than, more than or exactly n” — so -gid +1000 finds everything above a threshold. BSD documents it as a GNU-compatibility alias for -group.

-nogroup (“No group corresponds to file’s numeric group ID” — GNU) ran clean here and found nothing, the correct answer on a healthy machine. Creating a genuine orphan to prove the positive case needs root, so that half is unverified — I ran the primary, not a hit.

The group on a new file is often not the group you assumed

Before auditing, know how files got their group. On macOS a new file inherits the directory’s group, not your login group. Same shell, same user, two directories:

$ id -gn
staff
$ chgrp admin proj/docs
$ ls -l proj/src proj/docs | awk '{print $1, $4, $9}'
proj/docs:
-rw----r--@ admin notes.md
-rw-r-----@ admin readme.md

proj/src:
-rw-------@ wheel app.c
-rw-r--r--@ wheel lib.c

Not one of those files is staff. On Linux the same thing happens whenever the parent directory carries the setgid bit (chmod g+s dir) — which is how shared project trees are normally configured, so it is the common case. This is why a group audit turns up files nobody remembers assigning.

-perm 600 and -perm -600 are different questions

To find files that have read and write access for the owner, -perm 600 is the wrong test. It is an exact match on the whole mode, so it silently drops everything else you wanted:

$ find proj -type f -perm 600
proj/src/app.c

$ find proj -type f -perm -600
proj/dir with spaces/file name.txt
proj/docs/notes.md
proj/docs/readme.md
proj/src/app.c
proj/src/lib.c

GNU’s three forms: -perm mode is “exactly mode”, -perm -mode is “All of the permission bits mode are set”, -perm /mode is “Any of the permission bits mode are set”. Combining tests has a second trap: find proj -group wheel \( -type d -o -perm -200 \) means group wheel AND (directory OR owner-writable): six paths here, against nine for a plain OR.

Do not point -exec chmod at a tree you have not looked at

Consider find /home -group developers -type f -exec chmod 644 {} \;, or the same search with -exec rm {} \;. Here is what the first one does to a directory that contains an SSH key and an environment file — executed, on a scratch directory:

-- before --
-rw-------  .env
-rw-------  id_ed25519
-rw-r--r--  id_ed25519.pub

$ find secrets -type f -exec chmod 644 {} \;

-- after --
-rw-r--r--  .env
-rw-r--r--  id_ed25519
-rw-r--r--  id_ed25519.pub

A private key and a credentials file, both now world-readable, from one command that looked like tidying. ssh(1): these files “should be readable by the user but not accessible by others (read/write/execute). ssh will simply ignore a private key file if it is accessible by others.” Ignore, not warn — so the first symptom is an authentication failure with no explanation. Run the selection with -print and read it before you attach an action, use -ok for a per-file prompt, and exclude what must not be touched (-not -path '*/.ssh/*').

The ordering trap does not even need a typo — just the wrong word first. GNU find(1): “Don’t forget that find evaluates the command line as an expression, so putting -delete first will make find try to delete everything below the starting points you specified.” BSD does it too, without a warning:

$ find dtest -delete -name '*.tmp'
rc=0
before: 5 paths
after:  0 paths

Five paths in, nothing left — including the starting directory. With the filter first, find dtest -name '*.tmp' -delete, exactly the one .tmp file went. Prefer -exec … {} + to {} \; where the command allows it: over five files, \; forked five times, + once.

2>/dev/null is a real tradeoff, not a formality

A system-wide search as a normal user drowns in noise. Real counts from one run over /private/var:

exit=1  stdout_lines=4528  stderr_lines=247
find: /private/var/networkd/Library: Permission denied
find: /private/var/networkd/db: Operation not permitted

Note the exit status: 1, despite 4528 correct results. GNU documents the same rule — “find exits with status 0 if all files are processed successfully, greater than 0 if errors occur” — so find … && something will not run after a successful audit. And redirecting stderr hides more than noise. A mistyped starting path produces no output and no complaint:

$ find /privtae/var -group wheel 2>/dev/null
rc=1
$ find /privtae/var -group wheel
find: /privtae/var: No such file or directory

Send stderr to a file instead (find / -group ops -print 2> find.err) and skim it; it is the only way to tell “no such files exist” from “I could not look”. For the rest of find‘s syntax — symlinks, -name versus -regex, quoting — see the find command tutorial.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.