I want to copy the attributes (ownership, group, ACL, extended attributes, etc.) of one directory to another but not the directory contents itself.

This does not work:

cp -v --attributes-only A B
cp: omitting directory `A'           

Note: It does not have to be cp.

up vote 12 down vote accepted

After quite a bit of trial and error on the commandline, I think I've found the answer. But it isn't a cp-related answer.

rsync -ptgo -A -X -d --no-recursive --exclude=* first-dir/ second-dir

This does:

-p, --perms                 preserve permissions
-t, --times                 preserve modification times
-o, --owner                 preserve owner (super-user only)
-g, --group                 preserve group
-d, --dirs                  transfer directories without recursing
-A, --acls                  preserve ACLs (implies --perms)
-X, --xattrs                preserve extended attributes
    --no-recursive          disables recursion

For reference

    --no-OPTION             turn off an implied OPTION (e.g. --no-D)
-r, --recursive             recurse into directories
  • That last edit is definitely a hit there. With or without a / on the second-dir, that finally does exactly what is asked, it looks like. – killermist Jul 31 '12 at 18:50
  • The trailing slash only makes a difference on the first argument, not the second. It's explained in the 3rd or 4th paragraph of the USAGE section of the rsync(1) man page. – jw013 Jul 31 '12 at 18:51
  • @jw013 After reading the rsync documentation repeatedly, months ago, my conclusion was that the trailing slashes were an implied "into", whether in the first (second, third, etc) or last argument. But then again, I often end my commands with a / on the last argument to imply/demand an "into" affect. – killermist Jul 31 '12 at 18:58
  • My gut feeling to copy all directory attributes and no regular file would be rsync -a -AX --include='*/' --exclude='*'. I haven't tested. – Gilles Jul 31 '12 at 19:11
  • @jw013 Thanks on helping get this answer dialed in. I tried SO many options on my little sandbox test environment, and kept coming close, but having some part of it not work right. – killermist Aug 1 '12 at 17:04
chmod --reference=first-dir second-dir
  • 2
    This won't work recursively, and I believe it will not copy ACLs or extended attributes. – Mat Nov 29 '12 at 17:17
  • It's not supposed to be recursive: The OP asked for "not the directory contents itself", and the accepted answer tells rsync --no-recursive. The problem with this answer is that it doesn't do ACLs or XATTRs. – ShadSterling Oct 6 '16 at 12:51
cp -rfp from_dir to_dir
  • -r - recursive
  • -f - force
  • -p - preserve attributes: mode, ownership, timestamps
  • Also need --attributes-only – smremde Oct 14 '17 at 21:11

You need the "-r" for copying a directory.

  • 2
    I think I misunderstood you, you don't need the contents, just the xattr. If it was for SELinux, you could use chcon referencing the original directory (A). – tripledes Jul 29 '12 at 20:38
  • -r implies copy of all subdirectories and files in them... I do not want to copy whole tree... I want to copy attributes for only one and only one directory - without affecting it's contents. – Grzegorz Wierzowiecki Jul 29 '12 at 21:20

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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