2

I want to copy all files and folders from one directory to another without changing the permissions of the destination directory.

Where origin folder is 555 and destination folder is 775. If I use command below, the destination is changed to 555. I don't want this.

cp -r /origin/. /destination

What is correct syntax to copy contents of /origin? So that /destination remains 775

I do not have sudo.

5
  • can you show an ls of what you had before, show us what you did, and then an ls of what you have at the end (add to question). Commented Apr 8, 2017 at 10:00
  • @richard. Thanks, easier if I don't, as it will mean having to reinstall an application on a server Commented Apr 8, 2017 at 10:04
  • 1
    Easier not to answer you question: If you can not give the info I need then I can not answer. I currently have two answers (both opposites to each other); the correct answer depends on the info I have requested. I have not asked you to install anything. You just have to type (paste) ls -ld /origin /destination; cp -r /origin/. /destination; ls -la /destination Commented Apr 8, 2017 at 10:10
  • @richard Your help is appreciated and no offence was intended, it is a live website and I don't want to break it again. Thanks Commented Apr 8, 2017 at 10:13
  • You don't offend me. You just mystify me, if you do not go make, to the server, then all answers are equally correct ( It does not matter what you do-not-do ). Commented Apr 8, 2017 at 10:17

2 Answers 2

5

use rsync:

rsync -rlD /origin/ /destination/

The important thing is the / at the end of origin/, which tells rsync to copy the contents of the directory, not the directory itself. This way the toplevel directory isn't copied and /destination isn't changed except for files being added to it.

Switches explained:

-a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)

-r, --recursive             recurse into directories
-l, --links                 copy symlinks as symlinks
-p, --perms                 preserve permissions
-t, --times                 preserve modification times
-g, --group                 preserve group
-o, --owner                 preserve owner (super-user only)
-D                          same as --devices --specials

--devices               preserve device files (super-user only)
--specials              preserve special files
4
  • I've heard quite a bit about rsync and look forward to trying it Commented Apr 9, 2017 at 5:57
  • 1
    This did not preserve the target permissions. Commented Jan 11, 2021 at 21:32
  • true, adjust rsync switches. rsync -rlD.
    – Michael D.
    Commented Jan 22, 2021 at 11:22
  • @MichaelD. was it the effective removal of the -p from the -a that you had before the reason the permissions of the source folder weren't copied to the destination? What if the OP wanted the permissions of the CONTENTS of the source files to be copied without the permissions of the destination folder itself to be copied from the source folder.
    – poshest
    Commented Oct 26, 2022 at 11:12
0

Instead of copying the directory itself, copy the files in it.

cp -r /origin/* /destination

If there are dot files (files whose name begins with the character .), they won't be copied, because * skips those. Use cp -r /origin/.[!.]* /destination to copy the dot files, and cp -r /origin/..?* /destination if there are files whose name begins with .. (apart from .. itself, which designates the parent directory).

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .