Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
You did not pass -r, so it is ignoring directories. But I guess you want to copy only the attributes, not the contents? I can think of only ugly hacks. Also note that install can preserve SELinux contexts. – lynxlynxlynx Jul 29 '12 at 20:03
possible duplicate of Clone ownership and permissions from another file? – Gilles Jul 30 '12 at 0:22
@Gilles, thank you for pointing to another topic. It solves part of problem: ownership and mode. What about extended attributes? – Grzegorz Wierzowiecki Jul 31 '12 at 13:13
@GrzegorzWierzowiecki I'm afraid very few tools support extended attributes. If they're important, use killermist's rsync suggestion. – Gilles Jul 31 '12 at 19:08
up vote 10 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
share|improve this answer
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
cp -rfp from_dir to_dir
  • -r - recursive
  • -f - force
  • -p - preserve attributes: mode, ownership, timestamps
share|improve this answer
chmod --reference=first-dir second-dir
share|improve this answer
This won't work recursively, and I believe it will not copy ACLs or extended attributes. – Mat Nov 29 '12 at 17:17

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

share|improve this answer
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 posting your answer, you agree to the privacy policy and terms of service.

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