Quick take: Use cp to copy files and mv to move or rename them. Add -r to copy directories, -i to be prompted before overwriting, and -p to preserve timestamps and permissions.
Introduction
Copying and moving files are everyday tasks, and Linux handles them with two short commands: cp (copy) and mv (move). The same mv command also renames files, since renaming is just moving a file to a new name in the same directory.
This guide explains both commands together because they share most of their options and are almost always learned as a pair.
Syntax
The basic syntax of the cp and mv command is:
cp [OPTIONS] SOURCE DESTINATION
mv [OPTIONS] SOURCE DESTINATIONCommon Options and Parameters
The most useful options and parameters for the cp and mv command:
| Option | Description |
|---|---|
| -r, -R | Recursive — copy a directory and everything inside it (cp only). |
| -i | Interactive — prompt before overwriting an existing file. |
| -f | Force — overwrite without prompting. |
| -n | No-clobber — never overwrite an existing file. |
| -p | Preserve mode, ownership, and timestamps (cp). |
| -a | Archive — like -rp, preserve everything (cp, ideal for backups). |
| -u | Update — copy/move only when the source is newer than the destination. |
| -v | Verbose — print each file as it is processed. |
| -b | Back up each destination file before overwriting. |
Practical Examples
Real cp and mv commands you can run today:
# Copy a file
cp report.txt /backup/
# Copy a directory and its contents
cp -r mysite/ /var/www/
# Copy and preserve permissions and timestamps
cp -a config/ /etc/myapp/
# Prompt before overwriting
cp -i data.csv /backup/
# Move (or rename) a file
mv old-name.txt new-name.txt
# Move several files into a directory
mv *.log /var/log/archive/
# Move only if the source is newer
mv -u build/* /srv/app/Safe Copy and Move Workflows
Both cp and mv overwrite destinations silently by default, which is the single most common cause of accidental data loss on the command line. A few habits make them safe:
- Use
-i(interactive) when working with important files so you are prompted before any overwrite. - Use
-n(no-clobber) in scripts where overwriting must never happen. - Use
-bto keep a backup of every file that would be overwritten.
# Copy a config, keeping a backup of any existing one
cp -b nginx.conf /etc/nginx/nginx.conf
# Move logs only if newer, never overwriting newer files
mv -u -n *.log /var/log/archive/Common Mistakes to Avoid
The classic mistake is a trailing-slash or wildcard surprise. cp -r source dest copies the directory into dest if dest exists, but becomes dest if it does not — leading to an unexpected nested folder. Always check whether the destination already exists.
Another trap is moving files across filesystems: mv looks instant within one filesystem because it only rewrites a directory entry, but moving to a different disk or mount actually copies then deletes, which is slower and can fail midway if space runs out. For large cross-disk moves, prefer rsync with verification before removing the source.
Tips and Best Practices
- Use
cp -afor backups — it preserves permissions, ownership, timestamps, and symlinks in one flag. - Add
-i(or aliascp='cp -i') to avoid silently overwriting important files. mvacross the same filesystem is instant because it only updates the directory entry; moving across filesystems actually copies then deletes.
Final Thoughts
cp and mv are simple but worth treating carefully — both can overwrite files without warning. Reach for -i when you are unsure, -r for directories, and -a when copying anything you want to preserve exactly. Together they cover copying, moving, and renaming across the entire filesystem.
FAQ: cp and mv Command in Linux
How do I copy a directory in Linux?+
Use the -r (recursive) flag: cp -r sourcedir/ destdir/. For an exact copy that preserves permissions and timestamps, use cp -a sourcedir/ destdir/.
How do I rename a file in Linux?+
Use mv with the old and new names: mv oldname.txt newname.txt. Renaming is just moving a file to a new name in the same directory.
How do I stop cp or mv from overwriting files?+
Use -i to be prompted before each overwrite, or -n to never overwrite existing files. You can also alias the commands to include -i by default.
What is the difference between cp -p and cp -a?+
cp -p preserves mode, ownership, and timestamps. cp -a (archive) does that and also copies recursively and preserves symbolic links, making it the best choice for full backups.
Does mv copy or move the file?+
On the same filesystem, mv just renames the directory entry, so it is instant. Across different filesystems it copies the data and then removes the original.
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support