0

I have several shared folders and I need to find if a user has several folders there. e.g:

/shared/release/
/shared/compile/
/shared/tags/
/shared/general/

I need a bash script that will list those folders and tell me if a user has more than 1 owned folder.

Using bash, how can I get a list of these folders and determine if a user owns more than one?

  • Look at the man page for find and you can easily construct a script yourself. – mdpc Sep 28 '16 at 18:23
  • 2
    What is the question here? Please be so kind to at least read the tour, there it is explained that this is a Q&A site. Browsing the help center will indicate what is appropriate to post here and what is not. After that, edit your post, remove the irrelevant thanks, and include an interrogative sentence relevant for this site (please realise that "Can anyone help me?" is not a question within U&L's topics). Failure to make this post comply with the sites rules most likely will cause it to be closed for one reason or another, so in your own interest improve the question to an acceptable level. – Anthon Sep 28 '16 at 18:32
1

This will shown which users have more than one directory:

find /shared -type d -printf '%u\n' | sort | uniq -dc

This requires GNU find to support the -printf action. Your CentOS will have this.

This will list both the users with more than one directory and their directories:

find /shared -type d -printf '%u %p\0' | awk '{c[$1]++; a[$1]=a[$1](a[$1]?"\n  ":"")$0} END{for (user in c) if (c[user]>1) print c[user],a[user]}' RS="\0"

For maximum flexibility, we used NUL-separated output from find. To work with this, we need GNU awk or recent versions of mawk that supports NUL-separated input. Since you are using CentOS, this should be fine.

How it works

  • find /shared -type d -printf '%u %p\0'

    This tells find to look for directories under /shared and print out their owner, %u, along with the directory name, %p in a NUL-separated list.

  • awk '{c[$1]++; a[$1]=a[$1](a[$1]?"\n ":"")$0} END{for (user in c) if (c[user]>1) print c[user],a[user]}' RS="\0"

    This counts up the number of directories each user has and prints a report for users with more than one. In more detail:

    • c[$1]++ increments the count for the user specified in the first input field.

    • a[$1]=a[$1](a[$1]?"\n ":"")$0} saves the name of each directory found for this user.

    • END{for (user in c) if (c[user]>1) print c[user],a[user]} prints out the report.

    • RS="\0" tells awk to use NUL-separated input. This makes the process safe even if directory names contain newlines or other difficult characters.

2

Using find:

find /shared -mindepth 1 -maxdepth 1 -type d -user <username>

Replace <username> with the actual username you want to look for.

  • -maxdepth 1 will prevent recursive traversal i.e. will only look at /shared directory

  • -mindepth 1 will prevent listing the /shared directory itself from being listed if owned by <username>, if you don't want this behavior remove this option

  • -type d indicates we are only interested in directories

  • -user <username> indicates we are looking for the <username> owned files (directories)

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