Skip to content

Did you know?

cp -a copies a directory with its permissions, ownership and timestamps intact.

Plain cp -r gives the copies new timestamps and makes you the owner.

Explained

When you back up a configuration directory before a change, you want an exact copy. cp -r copies the files, but they get the current time as their modification date and belong to whoever ran the command. cp -a (archive mode) keeps everything:

sudo cp -a /etc/nginx /root/nginx-backup

-a is the same as -dR --preserve=all: it copies recursively, keeps symbolic links as links, and preserves permissions, ownership, timestamps and, where supported, extended attributes and SELinux contexts. Ownership can only be preserved when you run it as root.

Compare the two:

ls -l --time-style=long-iso /root/nginx-backup/nginx.conf

For copies to another server, or to update an existing copy, rsync -a does the same job and only transfers what changed. If you only need the timestamps and mode kept, cp -p is the lighter option.

Learn more

← All tech tips · Suggest a fix

Back to top