Copying and Renaming — cp and mv
Before she changes her harvest spreadsheet, Asha wants a spare copy. Later she wants to rename the whole plots folder to beds. Copying and renaming look alike on the command line, but they leave very different results behind.
The rule
cp leaves the original; mv takes it away.
cp makes a new, independent copy. The original stays exactly where it was:
$ cd ~/garden
$ cp harvest.csv harvest-backup.csv # copy under a new name
$ cp harvest.csv /tmp/ # copy into another folder
$ cp -r plots plots-2025 # copy a directory: needs -rRenaming in Linux is simply moving to a new name with mv (you met mv in section 1). There is no separate rename command in the basic toolkit:
$ mv plots beds # rename a folder
$ mv harvest.csv harvest-2026.csvOptions worth knowing for cp:
| Option | Does |
|---|---|
-r |
copy directories and everything inside them |
-i |
ask before overwriting an existing file |
-n |
never overwrite |
-p |
keep the original's timestamps and permissions |
-a |
archive mode: recursive and keep everything, which is best for backups |
-v |
print each file as it's copied |
Like mv, cp overwrites an existing destination without asking unless you add -i or -n.
To rename many files at once, a short loop does the job. Here every .txt in the folder becomes .md:
$ for f in *.txt; do mv "$f" "${f%.txt}.md"; done${f%.txt} means "the value of f with .txt cut off the end". Some systems also include a rename tool, but its options differ between distributions, so check man rename before using it.
The shortcut
Ask "do I still want the original here afterwards?" If yes, use cp. If no, use mv. Then, for either command, check whether the destination already exists, because that decides whether you get a new name, a copy inside a folder, or an overwrite.
First run
backupdoesn't exist yet, so cp createsbackupas a copy ofplots.
That nesting surprise catches everyone at least once. If you want to refresh the files inside an existing folder, copy its contents instead: cp -r plots/. backup/.
Drills
Answer fast — trust the rule.
After cp harvest.csv spare.csv, which files exist?
How do you rename plots to beds?
cp plots plots-old fails with "omitting directory". What's missing?
backup/ already exists. What does cp -r plots backup create?
Which command is best for a backup copy that keeps timestamps and permissions?
Remember
- cp leaves the original; mv takes it away. Renaming is just
mvto a new name. - Ask whether you still want the original, then check whether the destination already exists.
- Directories need
cp -r(or-afor backups), and both commands overwrite silently unless you add-ior-n.
