Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

So I was going to back up my home folder by copying it to an external drive as follows:

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

With the result that all folders on the external drives are now owned by root:root. How can I have cp keep the ownership and permissions from the original?

share|improve this question
8  
man cp would be a good starting point. –  Wojtek Rzepala Jul 20 '12 at 16:07
add comment

6 Answers 6

up vote 47 down vote accepted
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
share|improve this answer
add comment

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: http://stackoverflow.com/q/6339287/406686

Note the trailing slashes (see manpage for details).

share|improve this answer
2  
+1, cp -p is nice, but I like rsync's output so much more in general that I've aliased pcp to time rsync --progress -ah. Stands for "progress copy" in my mind. And both accept -r, so it works well for general terminal usage - but unfortunately, not in combination with sudo as shown in this question/answer. –  Izkata Jul 20 '12 at 17:59
add comment

I use cp -pdRx which will -p preserve mode, ownership & timestamps, -d preserve links (so you get symlinks instead the file contents copied), -R do it recursively and -x stay on one file system (only really useful if you're copying / or something with an active mount point).

PS: -R instead of -r is just habit from using ls -lR.

share|improve this answer
add comment

cp has an option to preserve file ownership. From the manual page of cp:

-p    Cause cp to preserve the following attributes of each source file in the copy: modification
      time, access time, file flags, file mode, user ID, and group ID, as allowed by permissions.
      Access Control Lists (ACLs) and Extended Attributes (EAs), including resource forks, will also
      be preserved.
share|improve this answer
add comment

You can do something like this:

tar cf - my_home | (cd /media/backup; sudo tar xf - )

tar keeps permissions, ownership and directory structure intact, but converts everything into a stream of bytes. You run a "subshell" (the parenthesized commands) that change directory, and then get tar to reverse the conversion. A steam of bytes becomes directories and files with correct ownership and permissions.

share|improve this answer
add comment

The answer is simple: cp has a -p option that preserves permissions (here's a fish).

But as Wojtek says in his comment, man cp (reading the fine manual) would be a good starting point (wanna learn how to fish?).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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