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.


6 Answers 6


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
    Commented Jul 31, 2012 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
    Commented Jul 31, 2012 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
    Commented Jul 31, 2012 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. Commented Jul 31, 2012 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
    Commented Aug 1, 2012 at 17:04
chmod --reference=first-dir second-dir
  • 3
    This won't work recursively, and I believe it will not copy ACLs or extended attributes.
    – Mat
    Commented Nov 29, 2012 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. Commented Oct 6, 2016 at 12:51
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 forgot hard links. Commented Apr 8, 2020 at 10:28
  • These commands just copy permissions/ownership/attributes of a single directory, without content, please read OP question. Hard links are not relevant here.
    – Alek
    Commented Apr 8, 2020 at 14:49
  • Correct. For me hard link or not is an attribute. Commented Apr 8, 2020 at 23:34
  • I'm not aware of the filesystems that support hard links to directories. Is there any? Since we are talking about directories, there's no need to specify -H, because the directory can't be a hard link, as far as I concerned. Correct me if I'm wrong.
    – Alek
    Commented Apr 9, 2020 at 17:20
  • The ext4 version used by android uses some directory hard links on the /data partition (at least in the case of Samsung). Commented Apr 9, 2020 at 17:26
cp -rfp from_dir to_dir
  • -r - recursive
  • -f - force
  • -p - preserve attributes: mode, ownership, timestamps
  • Also need --attributes-only
    – smremde
    Commented Oct 14, 2017 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. Commented Oct 13, 2018 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
    Commented Oct 13, 2018 at 23:24

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

  • 3
    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
    Commented Jul 29, 2012 at 20:38
  • 1
    -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. Commented Jul 29, 2012 at 21:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .