7

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..

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
csearle
  • 87
  • 1
  • 2

3 Answers3

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,892
  • 1
    I 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
  • 1
    @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
  • 67,283
  • 35
  • 116
  • 255
Nasir Riley
  • 11,422
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