cp (from "copy") is the standard Linux command for copying files and folders. It is in every distribution, so nothing needs to be installed.
Below are the main techniques useful when working on a server, from copying a single file to moving a site while preserving permissions. The examples are checked on Debian 12; on Ubuntu and other distributions with GNU coreutils they work the same.
Syntax
cp [options] source destinationIf the destination is an existing folder, the file is copied into it under the same name. If no such folder exists, cp creates a copy with the given name.
Basic examples
# Copy a file into a folder
cp config.yml /root/backup/
# Make a copy of a file next to it under another name, e.g. before editing settings
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
# Copy several files into one folder
cp file1.txt file2.txt /root/backup/
# Copy a folder with all its contents
cp -r /var/www/site /root/backup/Useful options
| Option | What it does |
|---|---|
-r | Copies folders whole, with all their contents |
-a | Archive mode. Copies folders and preserves permissions, owner, dates and links |
-p | Preserves the copy's permissions, owner and modification dates |
-i | Asks for confirmation before overwriting an existing file |
-n | Never overwrites existing files |
-u | Copies only if the source is newer or the file is missing at the destination |
-b | Before overwriting, keeps the old file with a tilde at the end of its name |
-v | Prints each copied file |
Copy a site while preserving permissions
To move sites and settings, use -a. The copy keeps its owner, permissions and dates, and the web server can work with the files without extra setup.
# Copy the contents of the site folder including hidden files
cp -a /var/www/site/. /var/www/site-copy/How to copy hidden files
The dot at the end of the path /var/www/site/. means "everything in the folder", including hidden files like .htaccess and .env. Do not use the .* pattern for this. In older bash versions it also captures .., that is the parent folder, and cp copies far more than you need.
Avoid overwriting files you need
# Ask before overwriting each file
cp -i new.conf /etc/app/app.conf
# Leave existing files alone, copy only the missing ones
cp -rn /root/backup/site/. /var/www/site/
# Overwrite, but keep the old version as app.conf~
cp -b new.conf /etc/app/app.confCopy only changed files
# Update the copy: only new and changed files are transferred
cp -au /var/www/site/. /root/backup/site/For regular copies and large folders, rsync is more convenient; it does the same and shows progress.
How to see copy progress
cp has no progress indicator, and the -v flag only prints file names. For large files and folders, use rsync.
# Install rsync if it is not there yet
apt install -y rsync
# Copy a folder with an overall progress indicator
rsync -a --info=progress2 /var/www/site/ /root/backup/site/Copying files between servers is convenient with scp or rsync; examples are in the article on backups.
Frequently asked questions
Error "-r not specified; omitting directory"
You are trying to copy a folder without the -r flag. Add -r or -a, for example cp -a /var/www/site /root/backup/.
Error "No space left on device"
The disk is out of space. Check how much is free with df -h, delete what you do not need or move to a plan with a larger disk. It is better to delete the partially copied file at the destination.
Error "Permission denied"
Your user has no rights to read the source or to write to the destination folder. Run the command through sudo or check the permissions with ls -l.
Can copying be interrupted?
Yes, press Ctrl + C. The files already copied stay in place, and the last file may end up copied only partially. To finish the rest later, run the command again with -u, or use rsync.
After copying, the site stopped working
Most likely the copy's owner changed because you copied without the -a flag. Restore the owner with chown -R www-data:www-data /var/www/site-copy on Ubuntu and Debian, or copy the folder again with -a.
Our resources
Telegram channel: https://t.me/f3cloud_news
Our website: https://f3cloud.com