Linux copy folders with permissions

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

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?

12 Answers 12

sudo cp -rp /home/my_home /media/backup/my_home 
 -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 

Much better to use cp -a . This also includes the -r ecursive flag, but it does more than that—it preserves everything about the file; SELinux attributes, links, xattr, everything. It’s «archive mode.» There are better tools for making a backup, but if you’re using cp for a backup, don’t use anything other than cp -a .

It works, but Patience is Good here. The command will only set everything right when it finishes: While it’s still copying a directory, and you’re running cp as root, the directory will be owned by root. It will only set it to the right permissions when it finishes with this directory.

cp -a doesn’t work on some systems: e.g. OS X, where (in some versions at least) one needed to use cp -pR . On my current OS X system though (10.10.15), cp -a seems to be honored.

If I use cp -a to copy a folder structure then use diff <(getfacl -R folder1) <(getfacl -R folder2) i sill seem to get different access control lists :(

sudo rsync -a /home/my_home/ /media/backup/my_home/ 
 -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).

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

NB: rsync -a , does not preserve extended attributes ( -X ) and no ACLs ( -A ) — the short description says archive mode; equals -rlptgoD (no -H,-A,-X) . E.g. SELinux contexts will not be preserverd without -X . For many use cases this is fine, but if you make a backup of your system partition, missing -X might break quite a lot. As far as I know, cp -a really preserves all file attributes.

Читайте также:  Discord voice changer linux

Just tested this, while sudo cp -a preserves ownership and groups, sudo rsync -a changes them into root. So, @Perseids is correct.

@JohnHamilton Under Mint, this works perfectly. it only changes ownership and groups later on (I can’t tell when). I’ve just copied my whole /home folder with rsync -aX /home /mnt/sdd/ and it worked like a charm.

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

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

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 .

Actually there is a difference between -r and -R . Check the man page (even the particular part too long to be quoted here).

I don’t think there is (although there may once have been and may still be on some versions of Unix). See man7.org/linux/man-pages/man1/cp.1.html It simply says -R, -r, —recursive copy directories recursively .

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.

@lucidbrot — I would think tar and cp -rp would differ only in corner cases — how they handle symbolic links or other special files, handling extended attributes of some filesystems, or dealing with sparse files.

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. 

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

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 

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.

Читайте также:  Can you install linux on raspberry pi

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

rsync is good for copying terabytes of data. it adds reusability. 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 

This answer seems to be exactly like the accepted answer (save the for sudo call, which however the OP seemes to be necessary . )

Also, you cannot preserve ownerships while copying unless you are root. Otherwise on systems with a per-user disk quota, it would allow an user to fill up other users’ disk quota by making copies of their files, as a form of a local denial-of-service attack.

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.

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 )

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.

Источник

How to Copy File Permissions and Ownership to Another File in Linux

Assuming you have two files or you have just created a new file and want it to have the same permissions and ownership of an older file.

In this article, we will show you how to copy permissions and ownership from one file to another file in Linux using chmod and chown commands respectively.

Copy File Permissions to Another File

To copy file permissions from one file to another file, use chmod command with the —reference switch in the following syntax, where reference_file is the file from which permissions will be copied rather than specifying mode (i.e octal or numerical mode permissions) for file.

$ chmod --reference=reference_file file
$ ls -l users.list $ ls -l keys.list $ sudo chmod --reference=users.list keys.list $ ls -l keys.list

Copy File Permissions to Another File

Copy File Ownership to Another File

Likewise, to copy ownership from another file, use chown command with the —reference switch as well using the following syntax, where reference_file is file from which owner and group will be copied rather than specifying owner:group values for file.

$ chown --reference=reference_file file
$ ls -l keys.list $ touch api.list $ ls -l keys.list $ sudo chown --reference=keys.list api.list $ ls -l api.list

Copy File Ownership to Another File

You can also copy file permissions and ownership from one file to multiple files as shown.

$ sudo chmod --reference=users.list users1.list users2.list users3.list $ sudo chown --reference=users.list users1.list users2.list users3.list

For more information, refer to the chown and chmod man pages.

Читайте также:  Посмотреть установленные пакеты linux ubuntu

You will also find these guides concerning file permissions to be useful:

That’s all! If you know any other way to copy or clone file permissions in Linux, do share with us via the feedback form below.

Источник

How to copy files and give them permission of destination directory

I am copying files from source to location. The source is not owned by me and the permission for files at source is —-rwx—. The permission of files coped to destination directory which is owned by me is —-r-x—. The permission of destination directory is drwxrwsrwx. How do I have the files with same permission of destination directory. I tried «cp —no-preserve=all» but it did not work (still the same permission).

2 Answers 2

cp --no-preserve=mode,ownership $backupfile $destination 

This does not set the destination directory’s permissions on newly copied files. Instead, it sets permissions of the user under which copy operation is done. For example, if you copy under root, copied files permissions will be root:root .

Let me rephrase that to «How to preserve permissions of destination directory on copy?»
I can’t take credit for the answer since I just combined a couple of answers I found on the wild. So here it comes.

Permissions are generally not propagated by the directory that files are being copied into, rather new permissions are controlled by the user’s umask. However when you copy a file from one location to another it’s a bit of a special case where the user’s umask is essentially ignored and the existing permissions on the file are preserved.

Which explains why you can’t directly propagate the permissions of the src to the dst directory.

However, there is two-step workaround to this.

  1. cp-metadata: Copy the attributes and only the attributes you want to preserve back to the source directory. Here is a quick script that can do this:
#!/bin/bash # Filename: cp-metadata myecho=echo src_path="$1" dst_path="$2" find "$src_path" | while read src_file; do dst_file="$dst_path$" $myecho chmod --reference="$src_file" "$dst_file" $myecho chown --reference="$src_file" "$dst_file" $myecho touch --reference="$src_file" "$dst_file" done 

You can leave out the touch command if you don’t want keep the timestamp. Replace myecho=echo with myecho= to actually perform the commands.
Mind that this script should be run in sudo mode in order to be able to run chown and chmod effectively

    cp —preserve : After you have successfully run the first command now it’s time to copy the contents along with the attributes to the dst directory.

—preserve[=ATTR_LIST]
preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all

Источник

Оцените статью
Adblock
detector