100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Bash

Copying, Moving, and Renaming

How cp and mv handle copying, moving, and renaming files and directories, including recursive copies, preserving attributes, and safe overwrite behavior.

Navigating & Managing FilesBeginner8 min readJul 9, 2026
Analogies

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.

bash
# 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.

  • cp duplicates files/directories, leaving the original intact; -r is required for directories, -a preserves full metadata.
  • mv both moves files between directories and renames them in place — Linux treats these as the same operation.
  • A same-filesystem mv is an atomic, near-instant metadata rename, not a data copy.
  • A cross-filesystem mv silently falls back to copy-then-delete and is not atomic.
  • Both cp and mv overwrite existing destinations by default with no warning unless -i or -n is used.
  • -v (verbose) on either command is useful for confirming exactly what happened during large or recursive operations.

Practice what you learned

Was this page helpful?

Topics covered

#Bash#LinuxShellScriptingStudyNotes#DevOps#CopyingMovingAndRenaming#Copying#Moving#Renaming#StudyNotes#SkillVeris#ExamPrep