Copying, Moving, and Renaming
Linux uses just two commands to cover copying, moving, and renaming files: cp (copy) duplicates files or directories, leaving the original in place, while mv (move) relocates a file or directory — and, notably, is also the mechanism used for renaming, since renaming is conceptually just 'moving' a file to a new name in the same directory. Both commands accept multiple source arguments followed by a single destination, and both behave differently depending on whether the destination is an existing directory or a new filename.
Cricket analogy: cp duplicating a scorecard while mv relocates it is like a statistician photocopying Sachin Tendulkar's career stats to archive them (cp) versus physically moving the original ledger to a new shelf and relabeling it (mv), the original never duplicated.
Copying with cp
cp source dest copies a single file. To copy a directory and its full contents, you must add -r (recursive); attempting to cp a directory without -r produces an 'omitting directory' error. The -a (archive) flag is commonly used for directory copies because it implies -r plus preservation of permissions, ownership, timestamps, and symbolic links — behaving much closer to what most people intuitively expect a 'full copy' to do. -i (interactive) prompts before overwriting an existing destination file, and -v (verbose) prints each file as it's copied, which is invaluable for large recursive operations.
Cricket analogy: Copying a directory without -r is like trying to transfer an entire team's training archive by grabbing only the top folder label, omitting every player's individual file inside — -a is the full squad transfer preserving every stat and history.
Moving and Renaming with mv
mv source dest is used for both moving a file to a different directory and renaming it in place — Linux doesn't distinguish between the two operations at the filesystem level when source and destination are on the same filesystem, since it's simply a metadata update (an atomic rename), not a data copy. When source and destination span different filesystems or mount points (e.g. moving from /home to a USB drive), mv transparently falls back to copy-then-delete, which is slower and, critically, not atomic — an interruption partway through can leave data in an inconsistent state. Like cp, mv supports -i for confirmation prompts and -v for verbose output.
Cricket analogy: Renaming a file with mv on the same filesystem is like a scorer instantly relabeling an innings from 'provisional' to 'final' with a single stroke of the pen — atomic and instant — while moving stats to an entirely different archive system means physically re-transcribing every entry, slower and riskier if interrupted.
# Copying
cp report.txt report_backup.txt # copy a single file
cp -r project/ project_backup/ # recursively copy a directory
cp -a project/ /mnt/backup/project/ # archive copy: preserves perms, ownership, timestamps, symlinks
cp -iv *.conf /etc/myapp/ # interactive + verbose, copying multiple files into a directory
# Moving and renaming
mv draft.txt final.txt # rename in place (same directory)
mv report.txt ~/Documents/ # move into another directory, keeping the same name
mv -i important.log important.log.bak # rename with an overwrite confirmation prompt
mv old_name_dir/ new_name_dir/ # rename a directory
# Multiple sources into one destination directory (must already exist)
mv file1.txt file2.txt file3.txt archive/Because a same-filesystem mv is just a metadata rename rather than a data copy, it completes almost instantly regardless of file size — moving a 50 GB file within the same disk partition takes a fraction of a second, while cp -r-ing the same file takes as long as writing 50 GB of new data. This is why mv is preferred over cp followed by rm whenever the source and destination share a filesystem.
By default, both cp and mv overwrite an existing destination file silently, without warning, if you have write permission to it. Always use -i (interactive) or -n (no-clobber, refuse to overwrite) when working with files you cannot afford to lose, especially in scripts run non-interactively where a prompt would hang forever — in scripts, prefer explicit existence checks or -n over -i.
cpduplicates files/directories, leaving the original intact;-ris required for directories,-apreserves full metadata.mvboth moves files between directories and renames them in place — Linux treats these as the same operation.- A same-filesystem
mvis an atomic, near-instant metadata rename, not a data copy. - A cross-filesystem
mvsilently falls back to copy-then-delete and is not atomic. - Both
cpandmvoverwrite existing destinations by default with no warning unless-ior-nis used. -v(verbose) on either command is useful for confirming exactly what happened during large or recursive operations.
Practice what you learned
1. What flag must be added to `cp` in order to copy an entire directory and its contents?
2. Why is a same-filesystem `mv` typically much faster than `cp -r` for a large file?
3. What happens when you `mv` a file from one mounted filesystem to another (e.g. from your root disk to a USB drive)?
4. Which flag preserves permissions, ownership, timestamps, and symbolic links during a copy?
5. What is the default behavior of `cp` and `mv` when the destination file already exists?
Was this page helpful?
Previous
Creating and Removing Files and Directories
Next
Viewing and Editing Files (cat, less, nano, vim basics)
You May Also Like
Creating and Removing Files and Directories
How to create empty files and directory trees with touch and mkdir, and safely remove them with rm and rmdir, including the dangers of recursive deletion.
Navigating the Filesystem (cd, pwd, ls)
Practical mastery of moving around the Linux directory tree using cd, pwd, and ls, including absolute vs relative paths and useful ls flags for everyday work.
Finding Files with find and locate
Master the two main Linux file-search tools: the real-time, highly flexible find command and the fast, index-based locate command, including when to use each.
Common Shell Scripting Pitfalls
A field guide to the mistakes that silently break Bash scripts — unquoted variables, wrong test operators, subshell scoping, and error-handling gaps.
Related Reading
Related Study Notes in DevOps
Browse all study notesNginx Study Notes
DevOps · 30 topics
DevOpsAnsible Study Notes
DevOps · 30 topics
DevOpsAdvanced Kubernetes Study Notes
Kubernetes · 30 topics
DevOpsAdvanced Bash Scripting Study Notes
Bash · 30 topics
DevOpsApache Kafka Study Notes
Kafka · 30 topics
DevOpsDocker & Kubernetes Study Notes
YAML · 40 topics