4

How can one list the content of a directory but only show directories and files that a user has Read access to?

I have worked out this but it only lists files owned:

find /dir/to/search -user johnsmith1 -ls

I was thinking a possible alternative was to "sudo su johnsmith1" and then ls or find but I am not sure if that is possible either.

3 Answers 3

7

List directories & files that a user (ubuntu in the examples) has read permissions to:

find -exec sudo -u ubuntu test -r '{}' \; -print

List directories & files that a user has write permissions to:

find -exec sudo -u ubuntu test -w '{}' \; -print

List directories & files that a user has execute permissions to:

find -exec sudo -u ubuntu test -x '{}' \; -print

List directories & files that a user has read, write & execute permissions to:

find -exec sudo -u ubuntu test -rwx '{}' \; -print
2
  • 2
    This is definitely the easiest and safest so far! The others did not factor in secondary groups.
    – PKCS12
    Commented Jun 4, 2019 at 9:00
  • To do this for the user being used, just omit the sudo part with the user, like this: find -exec test -x '{}' \; -print
    – Metafaniel
    Commented Aug 24, 2023 at 23:41
3

I found a way!

find -maxdepth 1 \( -type d -or -type f \) \( \( -user johnsmith1 -perm /u=r \) -o \( -group johnsmith1 -perm /g=r \) -o -perm /o=r \) -ls 
1
  • This will miss the case where a file is readable as a result of the user's secondary group memberships, I think? Commented May 1, 2019 at 11:54
1

You could do this by piping the output of ls into grep so if you only wanted to review directories or files which you have read permission only you could use this command

ls -l | grep [d,-]r--

if you wanted to review directories or files which you have read, write, and execution permissions you could use this command

ls -l | grep [d,-]rwx

if you wanted to review directories or files which you have read, and maybe other permissions you could use this command

ls -l | grep [d,-]r*
7
  • I don't think parsing ls output is a good approach - at the least you would need to anchor the matches to the start of the pattern. Your last expression in particular appears to confuse shell globs and regex - it will match the strings -r, ,r and dr (you probably meant [d-] not [d,-]) anywhere - including the user, group and filename. Also get into the habit of quoting patterns when they contain shell special characters such as * - otherwise you will get unexpected results when they happen to match filenames. Commented May 1, 2019 at 11:46
  • No that is precisely the match I wanted, dr* is for directories with read permision. -r * is for files with read permision. The brackets ensure that only strings starting with "-r" and "dr" will be matched. This will only provide secondary matches with filenames if those files include the required permissions because I'm piping from ls -l despite your unsubstantiated opinion about the approach. if you can demonstrate your claim ill be very surprised. Commented May 1, 2019 at 12:19
  • touch drat && chmod -r drat && ls -l | grep [d,-]r* Commented May 1, 2019 at 12:29
  • 'touch drat && chmod -r drat && echo 'grasping-at-straws' >> drat && ls -l | grep [d,-]r* ' 'more drat' > more: cannot open drat: Permission denied Commented May 1, 2019 at 12:35
  • your demonstration fails to meet the necessary criteria Commented May 1, 2019 at 12:37

You must log in to answer this question.

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