brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: cp -a keeps owner, group and permissions, plain cp -r does not

Permissions
~/tips/tip-permissions-cp-a-keeps-owner-mode

sudo cp -r /srv/site /srv/site.r

Copies a folder while keeping its owners, modes, timestamps and links.

A quick cp -r backup of a web folder looks fine until you restore it and the site can’t write its cache. The copy got your owner and your umask, not the original permissions.

Compare the two on the same folder:

sudo cp -r /srv/site /srv/site.r
sudo cp -a /srv/site /srv/site.a
ls -l /srv/site.r /srv/site.a

In a test with a file owned by another user and mode 666, the -r copy came out owned by root with mode 644. The -a copy kept the owner and the 666.

I’d use -a for every copy you might restore from. It stands for archive and means -dR --preserve=all: recursive, links kept as links, and mode, ownership, timestamps, ACLs and extended attributes kept as far as the target allows.

Ownership is only kept when you run it as root. As a normal user you can’t give files away, so cp -a keeps the modes but the files become yours. Copying to a FAT or exFAT USB stick or to some network shares also loses permissions, because those file systems can’t store them. For a copy to another machine, rsync -a does the same job and can resume.

Further reading: cp(1) manual page

More Permissions tips sudo su -s /bin/bash www-data sudo setfacl -R -d -m g:media:rwX /srv/media ls -ln /srv/data newgrp media sudo journalctl -k --since '10 min ago' | grep 'apparmor="DENIED"' All Permissions tips (30) All topics

Link