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..
Asked
Active
Viewed 3,737 times
3 Answers
14
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
steve
- 21,582
- 5
- 48
- 75
-
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.) – Peter Cordes Mar 15 '21 at 12:23
-
@PeterCordes Note that `-p` already preserves timestamps and `-a` is a non-POSIX extension. – nwellnhof Mar 15 '21 at 14:19
10
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.
Jeff Schaller
- 66,199
- 35
- 114
- 250
Nasir Riley
- 10,665
- 2
- 18
- 27
-
`-a` is also available for `cp` with the same meaning – val - disappointed in SE Mar 15 '21 at 12:36
3
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
Louis Thompson
- 116
- 1