Copy, move and rename files in Linux (cp and mv)
Use cp and mv to copy, move and rename files and folders on a Linux server, keep permissions intact with cp -a, avoid accidental overwrites and rename many files at once.
- Beginner
- Linux VPS
- 5 min read
Copying and moving files is something you do every day on a server: backing up a config before you edit it, moving an upload into a web folder, or renaming a log. Linux uses two commands for this, cp and mv. This guide shows how to use both safely, including how to copy whole folders, keep ownership and permissions, and rename many files in one go.
Before you start
- SSH access to your Linux VPS. See Connect to your Linux VPS with SSH.
- Know how to move around with
cdandls. See Navigate folders in Linux. - Use
sudoin front of a command when the files belong to root or another user.
By default, cp and mv overwrite an existing file with the same name without asking. There is no undo. Use -i (shown below) when you are not sure.
Copy a file with cp
The pattern is cp SOURCE DESTINATION:
cp notes.txt notes-copy.txtIf the destination is a folder, the file is copied into it with the same name:
cp notes.txt /home/deploy/backup/A common habit is copying a config file before changing it:
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bakYou can copy several files into a folder at once. The last argument must be the folder:
cp index.html style.css app.js /var/www/html/Copy a folder
cp refuses to copy a folder unless you add -r (recursive):
cp -r site/ site-backup/For backups and migrations, -a (archive) is usually better. It copies recursively and keeps ownership, permissions, timestamps and symbolic links:
sudo cp -a /var/www/example.com /var/www/example.com.bakWithout -a, copied files belong to whoever ran the command and get fresh timestamps. That can break a website whose files must belong to www-data or nginx.
A trailing slash on the source matters with some tools, such as rsync, but cp -r site/ dest/ and cp -r site dest/ behave the same: both copy the folder site into dest, or create dest as a copy if it does not exist yet.
Move and rename with mv
mv moves files. Renaming is just moving a file to a new name in the same folder:
mv report.txt report-2026.txt
mv report-2026.txt /home/deploy/archive/Unlike cp, mv works on folders without any extra option:
mv old-site/ /srv/archive/Moving within the same disk is instant, because only the name changes. Moving between disks copies the data and then deletes the original.
Avoid overwriting files
These options make cp and mv safer:
-i(interactive): asks before overwriting an existing file. Answeryorn.-n(no-clobber): never overwrites; existing files are skipped silently.-v(verbose): prints each file as it is copied or moved, so you can see what happened.-b(backup, GNU only): keeps the old file with a~suffix before overwriting it.
cp -iv config.yml /etc/myapp/
mv -iv upload.zip /var/www/html/If you want -i every time, add aliases to ~/.bashrc:
alias cp='cp -i'
alias mv='mv -i'Run source ~/.bashrc or log in again for them to take effect.
Rename many files at once
mv renames one file at a time. For many files, use a short loop. This example changes every .htm file in the current folder to .html:
for f in *.htm; do mv -iv -- "$f" "${f%.htm}.html"; done${f%.htm} removes .htm from the end of the name. The quotes protect names with spaces, and -- stops a name that begins with a dash from being read as an option.
There is also a dedicated rename command, but it differs between distributions. On Ubuntu and Debian, the rename package uses Perl expressions; on AlmaLinux and Rocky, rename from util-linux uses simple text replacement. Check man rename on your server before you rely on it. The loop above works the same everywhere.
Test a loop first by putting echo in front of mv. It prints each command without running it, so you can check the new names before anything changes.
Check it works
Use ls -l to confirm the result and compare details:
ls -l /var/www/example.com /var/www/example.com.bakAfter a cp -a, the owner, group, permissions and dates should match the original. To confirm two files are identical, compare them:
cmp original.conf original.conf.bak && echo "identical"Troubleshooting
- `cp: -r not specified; omitting directory`: you tried to copy a folder. Add
-ror-a. - `Permission denied`: you cannot read the source or write to the destination. Use
sudo, or check ownership withls -l. - The website broke after copying files: the new files are owned by the wrong user. Copy with
cp -a, or fix ownership withsudo chown -R www-data:www-data /var/www/example.com(usenginxorapacheon AlmaLinux and Rocky). - `No space left on device`: the destination disk is full. Check with
df -hand free space before you try again. - A file was overwritten by mistake: restore it from a backup. Next time, use
-ior-n.
Next steps
- Remove files you no longer need: Delete files and folders in Linux safely.
- Copy files between computers: Transfer files with SCP and SFTP.
- Learn the patterns that select many files: Wildcards in Linux.
- Need more room for backups? Compare Linux VPS plans.
Was this guide helpful?
Still stuck?
Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.
Related guides
Getting started
Edit files with nano: a beginner's guide
Open, edit, search and save files with the nano text editor on a Linux server, including cutting and pasting lines, showing line numbers and safely editing system config files with sudo.
- Beginner
- Linux VPS
- 5 min read
Getting started
Your first hour with a new RunRacks server
A step-by-step roadmap for the first hour after your RunRacks server is delivered, from finding your login details to changing the password, updating the OS and picking your next guides.
- Beginner
- All
- 5 min read
Getting started
How to choose a server location
Pick a data center location based on where your users, broker or team are, confirm it with ping tests, and weigh the legal and latency points that come with each choice.
- Beginner
- All
- 4 min read