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

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?

Asked By: Psachnodaimonia

||

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.

Answered By: user732

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.
Answered By: Matteo
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
Answered By: guido

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

Answered By: student

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.

Answered By: StarNamer

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 (want to learn how to fish?).

Answered By: buckdeer
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
Answered By: Zaz

you can use preserve=all, then your copy will keep all attributes like owner, group and timestamp of your files.
So, do your backup safely with the following command.

cp -r --preserve=all /home/my_home /media/backup/my_home
Answered By: Ario

The answer is

cp -rp /source/ /dest
Answered By: Alex

well ive using Linux Zorin and my issue was trying to copy over my home folder to an external drive while my computer is booted on a iso ( bootable usb drive ) as ive messed up my sdd and it now doest bootup properly so im doing a new install, this time i hope to install windows 7 then zorin OZ successfully or just Zorin OS, im not sure if i should dual boot or do linux with virtual machine.

what i did :

install caja ( through command line or software store )

run caja as root ( i used command line and run it as ROOT )

copy and paste my files i wanted, skipped the stuff that didnt want to copy and hopefully the ones i dont care about )

DONE.

for me 20gb of my home folder is taking forever, the few minutes it has been feels like an internity right now.

Hope this helps anyone even with all my rambling here.

Answered By: Abbir

I had a similar problem that I wanted to copy a large folder from one hard drive to a new one but took a while for me to consider the different partition formats.

The original was on an ext4 formatted partition on the extraction location was an external drive formatted as exFat. Once I realized this I formatted the external hard drive to ext4 and it worked, but I also used the -h flag for de-refenrencing symlinks.

tar -cf myfolder.tar myfolder
cd /path/to/new/harddrive
tar -xhf myfolder.tar

I was using Ubuntu 22.04, for additional details.

Wanted to share this experience in case someone else finds it helpful.

Answered By: Chef Pharaoh

rsync is good for copying terabytes of data. it adds resumability. And there are flags now to also copy extended attributes which is main issue in other comments.

rsync -aHAXS --info=progress2 --partial SOURCE_DIR DESTINATION_DIR

--hard-links, -H         preserve hard links
--acls, -A               preserve ACLs (implies --perms)
--xattrs, -X             preserve extended attributes
--sparse, -S             turn sequences of nulls into sparse blocks
--info=progress2         progress bar

source https://wiki.archlinux.org/title/rsync.

Answered By: Uzumaki D. Ichigo
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.