1

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.

1
find -exec sudo -u johnsmith1 test -r '{}' \; -print
  • This is definitely the easiest and safest so far! The others did not factor in secondary groups. – Questioner Jun 4 at 9:00
0

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*
  • 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. – steeldriver May 1 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. – Drew J-H May 1 at 12:19
  • touch drat && chmod -r drat && ls -l | grep [d,-]r* – steeldriver May 1 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 – Drew J-H May 1 at 12:35
  • your demonstration fails to meet the necessary criteria – Drew J-H May 1 at 12:37
0

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 
  • This will miss the case where a file is readable as a result of the user's secondary group memberships, I think? – steeldriver May 1 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.