Find all directories, in which user has access to search and echo this directories in shell

13,788

Solution 1

I wont give you the exact code but can give you the steps to achieve this:

  • Find the owner of the directory. If this matches with the user for which you are searching, then check the owner permissions on that directory. If it is r+x(5) or r+w+x(7), then the user can search inside that directory, otherwise not.

  • If the owner of the directory doesn't match with the user, check the group of the directory and find out whether the entered user belongs to that group or not. If the user belong. Check the group permissions on that directory. If it is r+x(5) or r+w+x(7), then the user can search inside that directory, otherwise not.

  • If the entered user doesn't belong to the group of the directory, check the others permissions on that directory. If it is r+x(5) or r+w+x(7), then the user can search inside that directory, otherwise not.

I think the above algo will perform the task which you want.

Edit:

Method 1:

You can use find command for that:

$ find / -maxdepth 1 -type d -perm -u=rx

This will find all directories inside / upto one level with user permission as atleast (read+execute).

Method 2:

$ stat <filename/directoryname>  | grep Access | head -1 | awk 'BEGIN{FS="[/)]"} {print $2}'  

This will give you the permissions in rwx notation (eg. drwxr-xr-x)

$ stat <filename/directoryname>  | grep Access | head -1 | awk 'BEGIN{FS="[(/]"} {print $2}'

This will give you the permissions in 755 notation (eg. 0755)

Solution 2

I am not sure, if I understand your question in its current form.

If you don't care about a sub-directory with read access being placed in a directory without read access, then you can simply do:

find . -type d -perm -u+r

If you want to know, whether a specific user has read access to a directory, then get the group(s) she/he is a member of:

groups $userID

And do:

find . -type d -group $groupID -perm -g+r
Share:
13,788

Related videos on Youtube

Alex Zern
Author by

Alex Zern

Updated on September 18, 2022

Comments

  • Alex Zern
    Alex Zern over 1 year

    I need to find all directories, in which user has access to search(command find) and echo this directories in shell. USER is read from ksh.

    for example:

    read user
    

    I know, if user want to use find in directory, user should have "read access" and "execute access" for this directory. I should check all directories for r and x access for inputed user.

  • Alex Zern
    Alex Zern about 11 years
    how to check permission of directory?
  • doneal24
    doneal24 about 11 years
    stat will give you file/directory permissions. In general, your script cannot find all the directories you want. As an example, if you have perms 711 on /a and perms 755 on /a/b (and you're not the owner), you cannot search /a but you can search /a/b.
  • pradeepchhetri
    pradeepchhetri about 11 years
    @AlexZern: I have updated the answer for your question.