You need to search for files that are RW by everyone OR are RW by the user's uid OR that are RW by all groups that the user is a member of. But it's a bit more complicated than that. As pointed out by here by Stephane Chazelas, if a file is owned by a user then ONLY the owner perms count, and if a file is owned by a group that the user is a member of then only the group perms count.
So we need to check for "owner=RW or (not owner and group=rw) or (not owner and not group and other=rw)"
You can get a list of the group gids that a user is a member of with id -G
. This can be used to construct a find command line with standard shell command substitution.
My original answer had a one-liner, but this jobs is far too complex to want to do in a one-liner. Here's a bash shell script that does the job.
#! /bin/bash
U="$1" # username to do perm search on - e.g. popo
TOPDIR="$2" # starting directory for search - e.g. /Users/Me/Desktop
# permissions to search for - defaults to "rw"
PERMS=${3:-rw}
# permission bits "style". "/"=ANY or "-"=ALL. defaults to /
#
# see find(1) and search for '-perm -mode' or '-perm /mode' for
# details on how this works.
PSTYLE=${4:-/}
# construct a find expression specifying all groups that the user
# is a member of
GIDS="$(for i in $(id -G "$U"); do echo -n " -gid $i -o "; done)"
GIDS=$(echo "$GIDS" | sed -e 's/ -o $//') # strip trailing " -o "
find "$TOPDIR" \
\( -user "$U" -perm ${PSTYLE}u=$PERMS \) \
-o \( -not -user "$U" -a \( $GIDS \) -perm ${PSTYLE}g=$PERMS \) \
-o \( -not -user "$U" -not \( $GIDS \) -perm ${PSTYLE}o=$PERMS \)
This script has to be run as root. Other users may not have the required permissions to look in all directories that popo can.
Note: I have tested this on a Debian Linux system using the latest versions of id
from GNU Coreutils and find
from GNU findutils. The Mac implementations of id
and find
may be different, I don't have a Mac available at the moment to test. If it is different, check the Mac man pages for id
and find
- the things to look for that might require modifying to suit a Mac are the -G
and -u
options for id
and the -perm /
or '-perm -' options of find
.
/-perm o+r -o
immediately before... -uid
. i've updated the answer. – cas Sep 19 '13 at 08:13-------rwx popo popo
while he hasn't. – Stéphane Chazelas Sep 19 '13 at 13:02chmod 7
on one of your files, and you lose access to it. See http://unix.stackexchange.com/a/88591/22565 – Stéphane Chazelas Sep 19 '13 at 15:18007
was just an example. And there's all the other issues mentioned at http://unix.stackexchange.com/a/88591/22565 where you'll also find a correct way to write thatfind
command. – Stéphane Chazelas Sep 19 '13 at 16:43