Getting started

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.

BeginnerLinux server5 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

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

Bash
cp notes.txt notes-copy.txt

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

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

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

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

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

Copy a folder

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

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

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

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

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

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

Bash
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.
Bash
cp -iv config.yml /etc/myapp/
mv -iv upload.zip /var/www/html/

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

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

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

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

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

Bash
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 MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.