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]

6 Answers 6

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

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

7
  • So -p is the better way to go than -a @Doon ? Oct 1, 2014 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 1, 2014 at 12:20
  • "better" -- I want to retain (or preserve) owner, group, permissions. "Better" means which one will do that for me. Oct 1, 2014 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 1, 2014 at 12:22
  • both of them do basically the same exact thing, depending upon your operating system / version of cp
    – Doon
    Oct 1, 2014 at 12:22
1

man cp says:

-p     same as --preserve=mode,ownership,timestamps
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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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