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

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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

Method 2

chmod --reference=first-dir second-dir

Method 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

Method 4

cp -rfp from_dir to_dir
  • -r – recursive
  • -f – force
  • -p – preserve attributes: mode, ownership, timestamps

Method 5

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

Method 6

You need the “-r” for copying a directory.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x