Search code examples
linuxaclls

Linux - list all subdirectories and get ACL


I am trying to:

  1. List all the subdirectories under a top level directory (/shared) and output this to a file called directory_list
  2. Afterwards I want to run getfacl using the file as input, and output the results to a file called acl_list

So ultimately, getting the ACLs for all subdirectories in /shared.

The steps I'm trying to use are:

  1. ls -d -- /shared/*/*/*/ > directory_list
  2. getfacl < directory_list > acl_list

Is there a different command I can use for (1) where it will list the subdirectories recursively, as -dr (list directories recursively) does not seem to work. Also I do not know how many levels of subdirectories there are.

When I run the 2nd command, there seems to be some syntax error preventing it from running, but I've checked the syntax from the linux man pages and it should be right.

Could anyone point me in the right direction?

Thanks


Solution

    1. Use find to do a recursive list:

      find /shared -type d -print > directory_list
      
    2. getfacl expects the filenames to be command line arguments, it doesn't read standard input. Use xargs to perform this translation:

      xargs -d '\n' getfacl < directory_list > acl_list
      

    The -d option specifies the delimiter, overriding the default use of whitespace. However, I believe this is a GNU extension, it may not be in all versions of xargs. If you can't use it, you'll have to write a loop:

    while read -r filename; do 
        getfacl "$filename"
    done < directory_list > acl_list