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.

  • 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). – ctrl-alt-delor Apr 8 '17 at 10:00
  • @richard. Thanks, easier if I don't, as it will mean having to reinstall an application on a server – MrPaulDriver Apr 8 '17 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 – ctrl-alt-delor Apr 8 '17 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 – MrPaulDriver Apr 8 '17 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 ). – ctrl-alt-delor Apr 8 '17 at 10:17

use rsync:

rsync -a /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
| improve this answer | |
  • I've heard quite a bit about rsync and look forward to trying it – MrPaulDriver Apr 9 '17 at 5:57

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.