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.
Copying with cp
cp SOURCE DEST copies a file. The original stays where it was.
cp drafts/essay-v1.txt drafts/essay-backup.txtIf 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-copyMoving 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.txtmv drafts/essay-old.txt final/mv moves directories too, without needing -r.
Careful: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 turn
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.
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)
The checklist ticks itself off as you work in the terminal: any way that gets the result counts.
Hints come one at a time, then one way to do it. Try each before the next.
What this lesson uses, in one place.
cp- Copy files and directories More on cp →
cp -r- Copy directories and everything in them More on cp →
mv- Move or rename files and directories More on mv →