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.


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 'SO- stop being evil' 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
  • 1
    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

I do not understand because it seems to work for others, but the rsync method fails for me on FreeBSD with ZFS. Nothing happens. However Jean-François Dockes’s method works. (see: https://www.lesbonscomptes.com/pxattr/ ) Source directory = A & destination directory = B, from the original question.

pxattr -lR A > tmp.EAs

(edit the first line of tmp.EAs to change the A directory to B, s/A/B/)

pxattr -S tmp.EAs
  • So, you’re not just recommending somebody else’s method; you’re recommending somebody else’s program, right? That’s OK, but I just wasted five minutes searching for documentation on a little-known non-standard program. It would be nice if you stated more clearly that you are recommending that people download a little-known non-standard program. – G-Man Says 'Reinstate Monica' Oct 13 '18 at 17:38
  • shrug It’s in the ports tree. That doesn’t exactly make it non-standard. It’s not as obscure as pyxattr and I tried that too. This was kind of a hard nut to crack. Supposedly gnu cp works for this, but I can’t make it go. Extended Attributes deserve better than this. Also, pxattr works at least under linux, osx, & bsd. – cira Oct 13 '18 at 23:24
rsync -aAX --exclude='*' src_dir/ dst_dir

where dst_dir - is a target dir. Or:

rsync -dADXgot src_dir dst_dir

where dst_dir - is a dir containing target dir, or a non-existing target dir.

From rsync man page:

    -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
        --exclude=PATTERN       exclude files matching PATTERN

    -d, --dirs                  transfer directories without recursing
    -p, --perms                 preserve permissions
    -A, --acls                  preserve ACLs (implies -p)
    -X, --xattrs                preserve extended attributes
    -o, --owner                 preserve owner (super-user only)
    -g, --group                 preserve group
        --devices               preserve device files (super-user only)
        --specials              preserve special files
    -D                          same as --devices --specials
    -t, --times                 preserve modification times

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 agree to our terms of service, privacy policy and cookie policy

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