Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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. I'm having a little trouble with this steps. Maybe a simple solution can help me and my algorithm is wrong. Please,help!

share|improve this question

2 Answers 2

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)

share|improve this answer
    
how to check permission of directory? –  Alex Zern Mar 29 '13 at 17:58
    
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. –  Doug O'Neal Mar 29 '13 at 18:12
    
@AlexZern: I have updated the answer for your question. –  pradeepchhetri Mar 29 '13 at 18:26

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|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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