How to clone/copy all file/directory attributes onto different file/directory?

27,908

Solution 1

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

Solution 2

chmod --reference=first-dir second-dir

Solution 3

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
Share:
27,908

Related videos on Youtube

Grzegorz Wierzowiecki
Author by

Grzegorz Wierzowiecki

Updated on September 18, 2022

Comments

  • Grzegorz Wierzowiecki
    Grzegorz Wierzowiecki over 1 year

    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.

  • tripledes
    tripledes almost 12 years
    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).
  • Grzegorz Wierzowiecki
    Grzegorz Wierzowiecki almost 12 years
    -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.
  • killermist
    killermist almost 12 years
    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.
  • jw013
    jw013 almost 12 years
    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.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 12 years
    My gut feeling to copy all directory attributes and no regular file would be rsync -a -AX --include='*/' --exclude='*'. I haven't tested.
  • killermist
    killermist almost 12 years
    @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.
  • Mat
    Mat over 11 years
    This won't work recursively, and I believe it will not copy ACLs or extended attributes.
  • nass
    nass about 9 years
    it is long , but it works ace!
  • Haddad
    Haddad over 7 years
    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.
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' over 5 years
    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.
  • user2284570
    user2284570 about 4 years
    You forgot hard links.
  • Alek
    Alek about 4 years
    These commands just copy permissions/ownership/attributes of a single directory, without content, please read OP question. Hard links are not relevant here.
  • user2284570
    user2284570 about 4 years
    Correct. For me hard link or not is an attribute.
  • Alek
    Alek about 4 years
    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.
  • user2284570
    user2284570 about 4 years
    The ext4 version used by android uses some directory hard links on the /data partition (at least in the case of Samsung).
  • Alek
    Alek about 4 years
    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.