Updated: 8 October 2026 · Applies to: GNU coreutils 9.4 to 9.7 and uutils coreutils 0.10.0 on Ubuntu 24.04 and 26.04, Debian 13 and AlmaLinux 10; rsync 3.2.7 to 3.5.0
To copy a file and keep its permissions, owner and timestamps, use cp -p; for a whole folder use cp -a. To give an existing file the same permissions and owner as another file, use chmod --reference=SOURCE TARGET and chown --reference=SOURCE TARGET.
Copy a file with its permissions (cp -p)
sudo cp -p ref.txt keep.txt
A plain cp gives the copy a new owner and the current time. -p keeps them. Our test, run as root:
$ cp ref.txt plain.txt $ cp -p ref.txt keep.txt $ ls -l --time-style=long-iso ref.txt plain.txt keep.txt -rw-r----- 1 alice alice 2 2024-01-02 03:04 keep.txt -rw-r----- 1 root root 2 2026-10-07 15:37 plain.txt -rw-r----- 1 alice alice 2 2024-01-02 03:04 ref.txt
-pis short for--preserve=mode,ownership,timestamps. You can name only what you need, for examplecp --preserve=mode,timestamps.- The mode includes ACLs: an extra
user:bob:r--entry on the source was kept on the copy. - Only root can keep another user's ownership. When an ordinary user ran
cp -p, the copy belonged to that user, the mode and timestamps were kept, and there was no error message.
Copy a folder with permissions (cp -a)
sudo cp -a /srv/site /srv/site-backup
-a (archive) is the same as -dR --preserve=all: it copies recursively, keeps symbolic links as links, and preserves modes, owners, timestamps, ACLs and other attributes on every file and folder. Use it for backups and moves within the same server.
Copy to another server with permissions
sudo rsync -aAX /srv/site/ user@server:/srv/site/
-akeeps permissions, timestamps, group and symbolic links. It keeps the owner only when rsync runs as root on the receiving side (rsync's help says "preserve owner (super-user only)").-adoes not include ACLs or extended attributes; add-Aand-X. In our test,rsync -aAXkept the ACL entry.- With tar, use
tar -cpfto pack andsudo tar -xpfto unpack. Extracted as root, our files came back with their original modes and owners.
More rsync options are in 13 rsync command examples.
Copy permissions from one file to another
- Copy the permission bits:
chmod --reference=ref.txt target.txt - Copy the owner and group:
sudo chown --reference=ref.txt target.txt - Check the result with
ls -l.
$ ls -l ref.txt target.txt -rw-r----- 1 alice alice 2 Jan 2 2024 ref.txt -rwxrwxrwx 1 root root 2 Oct 7 15:37 target.txt $ chmod --reference=ref.txt target.txt $ chown --reference=ref.txt target.txt $ ls -l ref.txt target.txt -rw-r----- 1 alice alice 2 Jan 2 2024 ref.txt -rw-r----- 1 alice alice 2 Oct 7 15:37 target.txt
Both commands accept several targets at once:
sudo chmod --reference=users.list users1.list users2.list users3.list sudo chown --reference=users.list users1.list users2.list users3.list
Other ways to do the same thing:
- Both in one command:
sudo cp --attributes-only --preserve=mode,ownership ref.txt target.txtcopies the mode and owner but leaves the contents oftarget.txtunchanged. - ACLs:
getfacl ref.txt | setfacl --set-file=- target.txtcopies the full ACL (install theaclpackage if the commands are missing). See how to configure ACLs on Linux. - Folders:
--referenceon a folder changes only that folder, not what is inside it. Avoidchmod -R --referencefrom a folder: in our test a plain file inside became-rwxrws---, the folder's mode with execute and setgid bits.
Copy permissions of a whole folder tree onto another
If you restored files that lost their permissions and still have an intact copy with the same layout, save the permissions from the good tree and apply them to the other one:
cd /srv/good && getfacl -R . > /tmp/perms.acl cd /srv/restored && sudo setfacl -P --restore=/tmp/perms.acl
Run as root, this restores the owner, group, mode and ACLs of every path listed in the file. The -P option stops it from following symbolic links; without it, AlmaLinux 10 prints a warning that the restore is unsafe.
Note for Ubuntu 26.04: chmod and chown there come from the Rust-based uutils coreutils 0.10.0, while cp is still GNU cp 9.7. Every command on this page gave the same result on Ubuntu 26.04, Ubuntu 24.04, Debian 13 and AlmaLinux 10.
Related: how to change the owner of a file with chown and what 755 and rwxr-xr-x mean.
Ucartz services for this topic
- KVM VPS hosting: NVMe SSD and full root access, with Free Basic Managed Support.
- Unmanaged dedicated servers: full root access and free IPMI KVM, with Free Basic Managed Support.
- Linux server management: hardening, patching, monitoring and automation for Ubuntu, Debian, RHEL and AlmaLinux.
