Skip to content

Copy, move and rename: cp and mv

Copy files with cp, copy folders with cp -r, and move or rename with mv. Practise each one on real-looking files in a terminal in your browser.

Lesson 5 of 167 minSkip to the terminal ↓

Copying with cp

cp SOURCE DEST copies a file. The original stays where it was.

cp drafts/essay-v1.txt drafts/essay-backup.txt

If the destination is an existing directory, the copy goes inside it and keeps its name: cp drafts/todo.txt final/ makes final/todo.txt.

Directories need -r (recursive): it copies the directory and everything inside it.

cp -r photos/holiday photos/holiday-copy

Moving and renaming with mv

mv SOURCE DEST moves. Within the same directory, moving is renaming; there is no separate rename command.

mv drafts/essay-v1.txt drafts/essay-old.txt
mv drafts/essay-old.txt final/

mv moves directories too, without needing -r.

Both cp and mv silently replace a file that already has the destination's name. Check with ls before you copy over something important.

Your task

Three jobs: copy drafts/essay-v2.txt to final/essay.txt (keeping the draft), rename drafts/todo.txt to drafts/done.txt, and make a copy of the whole photos/holiday folder called photos/holiday-backup.

  • Copy the essay to final/essay.txt (not done yet)
  • Rename todo.txt to done.txt (not done yet)
  • Back up photos/holiday as photos/holiday-backup (not done yet)

Lesson 5 of 16.

learner@sandbox: ~bash (practice)
Note: Practice shell for this lesson. Type a command and press Enter (help lists them all).