17

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.

bahamat
  • 39,666
  • 4
  • 75
  • 104

6 Answers6

15

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
jw013
  • 51,212
killermist
  • 1,601
  • 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 01 '12 at 17:04
  • it is long , but it works ace! – nass Apr 02 '15 at 15:25
1
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
Alek
  • 155
  • You forgot hard links. – user2284570 Apr 08 '20 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 Apr 08 '20 at 14:49
  • Correct. For me hard link or not is an attribute. – user2284570 Apr 08 '20 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 Apr 09 '20 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). – user2284570 Apr 09 '20 at 17:26
  • Thanks for the information. But I tend to disagree with you that hard link or not is an attribute, it is more related to the data structure/topology, rather than to attributes of the filesystem object. I think what you suggested is hardly desired in most cases, including OP's question, furthermore rsync does not supported that as far as I know. – Alek Apr 23 '20 at 23:46
1
chmod --reference=first-dir second-dir
  • 3
    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 06 '16 at 12:51
0
cp -rfp from_dir to_dir
  • -r - recursive
  • -f - force
  • -p - preserve attributes: mode, ownership, timestamps
slm
  • 369,824
burtsevyg
  • 119
0

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
cira
  • 1
  • 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
-3

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

tripledes
  • 1,207
  • 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 Jul 29 '12 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. – Grzegorz Wierzowiecki Jul 29 '12 at 21:20