Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The cp Commands You Will Use Most

cp config.php config.php.bak
cp report.pdf ~/backups/
cp -r site site-copy
sudo cp -a /var/www/site /var/backups/site-2026-11-28

The source comes first, the destination second. If the destination is an existing folder, the file is copied into it with the same name; otherwise the copy gets the destination name. Folders need -r or -a.

Existing Files Are Overwritten Without a Question

If a file with the destination name already exists, cp replaces its contents and does not ask. On a server that can mean losing a live configuration file. Two options prevent it:

sudo cp -i new.conf /etc/app/app.conf
sudo cp -n new.conf /etc/app/app.conf
  • -i: asks before overwriting; answer y to replace.
  • -n: never overwrites an existing file and copies only what is missing. On Debian 13, Ubuntu 26.04, and AlmaLinux and Rocky Linux 10, cp marks -n as deprecated; there and on Ubuntu 24.04, --update=none gives the same result.

Some distributions set alias cp='cp -i' for the root user, so cp may ask on one server and not on another. type cp shows whether an alias is active.

Copy Directories

cp -r site backup
cp -r site/. backup/

The result of the first command depends on whether backup already exists. If it does not, cp creates it as a copy of site. If it does, you get backup/site. A trailing slash on the source does not change this for cp, unlike rsync. To copy the contents of site into an existing folder, hidden files included, use site/. as the source.

Keep Owners, Permissions, and Timestamps

A plain copy belongs to the user who runs cp and gets the current time. -a (archive) copies folders recursively and keeps permissions, timestamps, symbolic links, and, when run as root, the owner and group. Use it for website folders and anything a service reads:

sudo cp -a /var/www/example.com /var/www/example.com.bak

-p keeps mode, ownership, and timestamps for single files without making the copy recursive.

Other Useful Options

OptionWhat it does
-uCopies only when the source is newer, or the destination is missing.
-vPrints each file it copies.
-t dirNames the target folder first.
-LCopies the file a link points to, not the link.

To copy several files at once, list them and end with a folder: cp a.txt b.txt c.txt ~/backups/.

Keep a Backup of What You Replace

sudo cp -b nginx.conf /etc/nginx/nginx.conf
sudo cp --backup=numbered nginx.conf /etc/nginx/nginx.conf

-b keeps the old destination as nginx.conf~ before copying, unless numbered backups of it already exist. --backup=numbered keeps every version as nginx.conf.~1~, nginx.conf.~2~, and so on. After replacing a configuration file, test it with the service's own check, such as sudo nginx -t, before you reload.

Copy to Another Server

cp works only on the same machine. To copy over the network, use scp or rsync over SSH, as described in how to use SCP. For moving instead of copying, see the mv command.

Common Errors

  • Omitting directory: you copied a folder without -r or -a.
  • Permission denied: you cannot read the source or write to the destination folder. Check with ls -ld, then use sudo if needed.
  • No space left on device: the disk is full; check with df -h before large copies, as shown in how to check disk space in Linux.
  • Cannot copy a directory into itself: the destination lies inside the source. Choose a folder outside it.
Was this answer helpful?
Loading...