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

By RunRacks Team · Published

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

Copy a file with cp

The pattern is cp SOURCE DESTINATION:

Terminal
cp notes.txt notes-copy.txt

If the destination is a folder, the file is copied into it with the same name:

Terminal
cp notes.txt /home/deploy/backup/

A common habit is copying a config file before changing it:

Terminal
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

You can copy several files into a folder at once. The last argument must be the folder:

Terminal
cp index.html style.css app.js /var/www/html/

Copy a folder

cp refuses to copy a folder unless you add -r (recursive):

Terminal
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:

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

Without -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.

Move and rename with mv

mv moves files. Renaming is just moving a file to a new name in the same folder:

Terminal
mv report.txt report-2026.txt
mv report-2026.txt /home/deploy/archive/

Unlike cp, mv works on folders without any extra option:

Terminal
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. Answer y or n.
  • -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.
Terminal
cp -iv config.yml /etc/myapp/
mv -iv upload.zip /var/www/html/

If you want -i every time, add aliases to ~/.bashrc:

Terminal
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:

Terminal
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.

Check it works

Use ls -l to confirm the result and compare details:

Terminal
ls -l /var/www/example.com /var/www/example.com.bak

After a cp -a, the owner, group, permissions and dates should match the original. To confirm two files are identical, compare them:

Terminal
cmp original.conf original.conf.bak && echo "identical"

Troubleshooting

  • `cp: -r not specified; omitting directory`: you tried to copy a folder. Add -r or -a.
  • `Permission denied`: you cannot read the source or write to the destination. Use sudo, or check ownership with ls -l.
  • The website broke after copying files: the new files are owned by the wrong user. Copy with cp -a, or fix ownership with sudo chown -R www-data:www-data /var/www/example.com (use nginx or apache on AlmaLinux and Rocky).
  • `No space left on device`: the destination disk is full. Check with df -h and free space before you try again.
  • A file was overwritten by mistake: restore it from a backup. Next time, use -i or -n.

Next steps

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.

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