I am having an issue where I have Parent folder that has full permissions.. I can create a new folder and that folder also has full permissions. However when I copy a folder over to this parent Directory and try to create a new directory to this copied directory. it loses all permissions.. is there a way to retain permissions to the copied folders..
3 Answers
Yes. When copying using cp
, the -p
option preserves permissions.
https://man7.org/linux/man-pages/man1/cp.1.html
-p same as --preserve=mode,ownership,timestamps
--preserve[=ATTR_LIST]
preserve the specified attributes (default:
mode,ownership,timestamps), if possible additional
attributes: context, links, xattr, all
-
1I normally use
cp -a
to preserve modtime as well. (And to enable recursive copying, in the rare case I'm using cp for that instead of rsync to preserve hardlinks.) Mar 15, 2021 at 12:23 -
@PeterCordes Note that
-p
already preserves timestamps and-a
is a non-POSIX extension. Mar 15, 2021 at 14:19
In addition to steve's answer, you can use rsync
.
rsync -avhH /path/to/source /path/to/destination
The a
switch preserves permissions, modify times, ownership, and also makes it recursive and copies symlinks. v
makes it verbose and h
and H
respectively make the output human readable and copies hardlinks.
The a
is important as that does what you want.
I like a tar pipe for retaining user/group ownership and permissions, and for tar's flexibility for defining which files to copy
tar cf - -C sourcedir -T filelist | tar xvf - -C targetdir