Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Working with Files  ›  Lesson

Copying and Renaming — cp and mv

Copying and Renaming 12 mincp makes an independent copy and leaves the original, mv renames or moves it, and whether the destination already exists decides where the result lands.

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 -r

Renaming 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.csv

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

`cp -r plots backup` run twiceStep 1 of 4
  1. First run

    backup doesn't exist yet, so cp creates backup as a copy of plots.

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.

quick drill

After cp harvest.csv spare.csv, which files exist?

quick drill

How do you rename plots to beds?

quick drill

cp plots plots-old fails with "omitting directory". What's missing?

quick drill

backup/ already exists. What does cp -r plots backup create?

quick drill

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 mv to a new name.
  • Ask whether you still want the original, then check whether the destination already exists.
  • Directories need cp -r (or -a for backups), and both commands overwrite silently unless you add -i or -n.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainThe nested copy surprise

Asha ran cp -r plots backup twice and ended up with backup/plots inside backup. Explain why, how to refresh the backup properly, and when she should use cp -a or mv instead.