How do I copy a folder keeping owners and permissions intact?

sudo cp -rp /home/my_home /media/backup/my_home

From cp manpage:

 -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

You can also use rsync.

sudo rsync -a /home/my_home/ /media/backup/my_home/

From the rsync manpage:

 -a, --archive
              This  is  equivalent  to  -rlptgoD.  It  is a quick way of saying you want
              recursion and want to preserve almost everything (with -H being a  notable
              omission).    The   only  exception  to  the  above  equivalence  is  when
              --files-from is specified, in which case -r is not implied.

              Note that -a does not preserve hardlinks, because finding  multiply-linked
              files is expensive.  You must separately specify -H.

See this question for a comparison between cp and rsync: https://stackoverflow.com/q/6339287/406686

Note the trailing slashes (see manpage for details).


cp -a

Where -a is short for --archive — basically it copies a directory exactly as it is; the files retain all their attributes, and symlinks are not dereferenced (-d).

From man cp:

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

Tags:

Permissions

Cp