Skip to the workspace
Pebellslearn by building
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.

linux / Copying and Renaming — cp and mv

Explore each scene at your own pace.

01 · The rule

The rule

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.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.