3

I just copied an entire directory structure from one location on my server to another:

cp -r /home/abc/public_html/* /home/xyz/public_html/

That worked fine. Except the dirs & files are now owned by root and the group is root as well.

How do I perform this copy action and retain the dirs & files ownership, groups, and permission settings?

Here is my man cp

NAME
       cp - copy files and directories

SYNOPSIS
       cp [OPTION]... [-T] SOURCE DEST
       cp [OPTION]... SOURCE... DIRECTORY
       cp [OPTION]... -t DIRECTORY SOURCE...

DESCRIPTION
       Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

       Mandatory arguments to long options are mandatory for short options too.

       -a, --archive
              same as -dR --preserve=all

       --backup[=CONTROL]
              make a backup of each existing destination file

       -b     like --backup but does not accept an argument

       --copy-contents
              copy contents of special files when recursive

       -d     same as --no-dereference --preserve=links

       -f, --force
              if an existing destination file cannot be opened, remove it and try again (redundant if the -n option is used)

       -i, --interactive
              prompt before overwrite (overrides a previous -n option)

       -H     follow command-line symbolic links in SOURCE

       -l, --link
              link files instead of copying

       -L, --dereference
              always follow symbolic links in SOURCE

       -n, --no-clobber
              do not overwrite an existing file (overrides a previous -i option)

       -P, --no-dereference
              never follow symbolic links in SOURCE

       -p     same as --preserve=mode,ownership,timestamps

       --preserve[=ATTR_LIST]
H. Ferrence
  • 7,906
  • 31
  • 98
  • 161

6 Answers6

2

Use cp -a to copy permissions and user/group.

The man page explains it:

-a, --archive
    same as -dR --preserve=all
...
-d     same as --no-dereference --preserve=links
...
-R, -r, --recursive
   copy directories recursively
...
--preserve[=ATTR_LIST]
   preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes:
   context, links, xattr, all
Joakim Nohlgård
  • 1,832
  • 16
  • 16
2

The answer "cp -a" is correct for some unix-related operating systems (for example Linux with "cp" from coreutils), but not all.

"cp -rp" is correct for some other.

You should consult the man page for "cp" on your specific operating system. or let us know what you are using.

tlund
  • 132
  • 7
1

from man cp

-p    Cause cp to preserve the following attributes of each source file
       in the copy: modification time, access time, file flags, file mode,
       ACL, user ID, and group ID, as allowed by permissions.

so cp -pr

Doon
  • 19,719
  • 3
  • 40
  • 44
  • So `-p` is the better way to go than `-a` @Doon ? – H. Ferrence Oct 01 '14 at 12:17
  • define better? `-a` == `-RpP` , so your case to use -a it would be `cp -a /home/abc/public_html/ /home/xyz/public_html/` since you point -a at a directory, not a file list. but beware with -a symbolic links will not be followed, so that may or may not be what you want. – Doon Oct 01 '14 at 12:20
  • "better" -- I want to retain (or preserve) owner, group, permissions. "Better" means which one will do that for me. – H. Ferrence Oct 01 '14 at 12:21
  • and to further confuse things You will need to check with your version of cp (since the BSD version appears to use slightly different options) see @tlund's answer) – Doon Oct 01 '14 at 12:22
  • both of them do basically the same exact thing, depending upon your operating system / version of cp – Doon Oct 01 '14 at 12:22
  • I normally do something like `tar cf - . | (cd target/dir && tar xf - )` which will preserve everything as well, And I can use it across ssh easily. Could just be that old habits die hard... – Doon Oct 01 '14 at 12:25
  • `cp -rp` did it for me. Thanks @Doon ! – H. Ferrence Oct 01 '14 at 12:27
1

man cp says:

-p     same as --preserve=mode,ownership,timestamps
jotrocken
  • 2,263
  • 3
  • 27
  • 38
0

As an aside, avoid:

cp -r src/* dest

because it doesn't copy hidden files

On linux do:

cp -rT src dest

and on BSD:

cp -R src/ dest
xpusostomos
  • 1,309
  • 11
  • 15
0

We can try something like below.

cp "/BCAN C2/Configs/script/" /mnt/Additional_Backup/scripts/ --recursive

Note: Use double quotes(") if your path contains whitespace.

Aashish Kumar
  • 2,771
  • 3
  • 28
  • 43