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
Related videos on Youtube
Author by
Grzegorz Wierzowiecki
Updated on September 18, 2022Comments
-
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
.-
lynxlynxlynx almost 12 yearsYou 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 thatinstall
can preserve SELinux contexts. -
Gilles 'SO- stop being evil' almost 12 yearspossible duplicate of Clone ownership and permissions from another file?
-
Grzegorz Wierzowiecki almost 12 years@Gilles, thank you for pointing to another topic. It solves part of problem: ownership and mode. What about extended attributes?
-
Gilles 'SO- stop being evil' almost 12 years@GrzegorzWierzowiecki I'm afraid very few tools support extended attributes. If they're important, use killermist's rsync suggestion.
-
Grzegorz Wierzowiecki over 6 yearsBtw. Here are nice examples where one may see xattrs in action: GUI of Dolphin in KDE: imgur.com/oguuJNm or URL from where file was downloaded set by browsers: references: gist.github.com/gwpl/a00e18bc150f1e3518335d13c5e46ad7 + freedesktop.org/wiki/CommonExtendedAttributes + vhanda.in/blog/2014/07/tagging-your-files
-
Sohail Si over 2 years
-
-
tripledes almost 12 yearsI 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 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 almost 12 yearsThat 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 almost 12 yearsThe 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 thersync(1)
man page. -
Gilles 'SO- stop being evil' almost 12 yearsMy gut feeling to copy all directory attributes and no regular file would be
rsync -a -AX --include='*/' --exclude='*'
. I haven't tested. -
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 over 11 yearsThis won't work recursively, and I believe it will not copy ACLs or extended attributes.
-
nass about 9 yearsit is long , but it works ace!
-
Haddad over 7 yearsIt'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' over 5 yearsSo, 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 about 4 yearsYou forgot hard links.
-
Alek about 4 yearsThese commands just copy permissions/ownership/attributes of a single directory, without content, please read OP question. Hard links are not relevant here.
-
user2284570 about 4 yearsCorrect. For me hard link or not is an attribute.
-
Alek about 4 yearsI'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 about 4 yearsThe ext4 version used by android uses some directory hard links on the
/data
partition (at least in the case of Samsung). -
Alek about 4 yearsThanks 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.