Linux copy with all permissions

How to copy permissions from one file to another on Linux

Syntax to clone permissions from another file or directory on Linux

The syntax is as follows use RFILE’s mode instead of MODE values:
chmod —reference= RRFILE FILE
chmod [options] —reference= RRFILE FILE

Examples: Copy file permission, but not files

Let us list both files:
ls -l install58.iso xenial-server-amd64.iso
Sample outputs:

To copy install58.iso file permission to xenial-server-amd64.iso, enter:
chmod —reference= install58.iso xenial-server-amd64.iso
Verify it:
ls -l install58.iso xenial-server-amd64.iso
Sample outputs:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

You can specify multiple files too:
chmod —reference= file.txt dest1.txt dest2.txt dest3.conf
You can combine and use find and xargs as follows:
find /path/to/dest/ -type f -print0 | xargs -O -I <> chmod —reference=/path/to/rfile.txt <>

Syntax to clone ownership from another file or directory on Linux

The syntax is as follows to use RRFILE’s owner and group rather than specifying OWNER:GROUP values
chown —reference= RRFILE FILE
chown [options] —reference= RRFILE FILE

Examples: Copy file ownership, but not files

To copy install58.iso file user and group onwership to xenial-server-amd64.iso, enter:
chown —reference= install58.iso xenial-server-amd64.iso
ls -l
Sample outputs:

Fig.01: Linux clone or copy or replicate file permissions, using another file as reference

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

I had never thought about needing this before and now that I see how it’s done can’t believe I haven’t used chmod/chown in this way before. Thanks!

You can acieve this same result in single command using ACL

getfacl file1 | setfacl –set-file=- file2

drw-r–r–. 2 emily emily 4096 Mar 18 14:58 z_dir
-rw-rw-r–. 1 root emily 0 Mar 18 14:56 z_file1
-rw-rw-r–. 1 emily root 0 Mar 18 14:56 z_file2
[root@oc1700126186:/home/emily]# ll z_dir
total 0
-rw-r–r–. 1 root root 0 Mar 18 14:58 zfile3
-rw-r–r–. 1 root root 0 Mar 18 14:58 zfile4

copy permission from file to directory recursively
—————————————————
[root@oc1700126186:/home/emily]# chmod -R –reference=z_file2 z_dir
[root@oc1700126186:/home/emily]# ll z_dir
total 0
-rw-rw-r–. 1 root root 0 Mar 18 14:58 zfile3
-rw-rw-r–. 1 root root 0 Mar 18 14:58 zfile4

copy ownership from file to directory recursively
————————————————–
[root@oc1700126186:/home/emily]# chown -R –reference=z_file2 z_dir
[root@oc1700126186:/home/emily]# ll z_dir
total 0
-rw-rw-r–. 1 emily root 0 Mar 18 14:58 zfile3
-rw-rw-r–. 1 emily root 0 Mar 18 14:58 zfile4
[root@oc1700126186:/home/emily]#

copy for multiple directory/files
———————————-
[root@oc1700126106:/home/cleri]# chown -R –reference=z_file2 z_dir1 z_dir2 z_dir3
[root@oc1700126106:/home/cleri]# chmod -R –reference=z_file2 z_dir1 z_dir2 z_dir4

find . | cpio -pVmud /destination/directory

I wonder, can this be used to clone a “home” folder from a failing drive and placed in a new install of the same OS?….will there be UUID conflicts?…what about other unique information?

for cloning a home directory, including permissions, use rsync:
% rsync -avP /source/ /destination/

Источник

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.

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.

Copy File Ownership to Another File

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

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

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.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

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

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:

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

\cp -rfp $src_dir $dst_dir should do what you want.

Источник

Linux:Copy, change permissions and ownership of a file in a command

Today I came across a requirement to copy couple of files and change permissions to execute those files when writing some ansible playbooks. We can do this by using cp, chmod, chowncommand as shown below. Ownership is changed from root user to normal user surendra.

-rw-r–r– 1 root root 0 Jun 10 21:22 abc.sh

Output:
-rwxr-xr-x 1 surendra surendra 0 Jun 10 21:23 /tmp/abc.sh

I can execute these commands with out any problem, but if you want to do same activity on a regular basis then it is time consuming task and more over keeping these commands in ansible play-book is not a great option. How about if we have a command in Linux which do all these stuff in one shot? Yes, we have a command called “install” which do this all in one command. The equivalent install command of above three commands is below.

To understand above command we should know couple of options for install command. Below examples will help you to understand install command with ease.

Example: Just copy a file from one location to other and do not bother about permissions and ownership.

Example: Copy a file with different permissions, for example every user in my machine should have execute permission on the file

Note: -m option arguments will be similar to chmod arguments.

Example: Copy a file with different permissions, for example full permissions for a file for all the users.

Example: Copy a file with different owner name

Example: Copy a file with different group owner

Источник

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:

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?

10 Answers 10

From cp manpage:

You can also use rsync .

From the rsync manpage:

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

Note the trailing slashes (see manpage for details).

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

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 .

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

You can do something like this:

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.

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.

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.

Источник

Читайте также:  Lenovo e440 windows 10
Оцените статью