Terminal start Lesson 6 of 12
Copying, moving and renaming
10 minutes
Learning goals
- Copy a file with cp and a whole folder with cp -r.
- Move and rename files with mv, and see why both are the same thing.
- Check the target before you copy or move, and let cp -i and mv -i ask before they overwrite.
Copying: cp
cp takes two things: what to copy (the source) and where to (the target). If the target is a new name, you get a copy
under that name:
cp notes.txt Documents/notes-copy.txt
Purpose: Copies a file. The original stays where it is.
Syntax: cp [OPTION]... SOURCE TARGET
Options:
Example:
cp notes.txt Documents/notes-copy.txt
ls Documents
'Holiday 2027.txt' letter.txt notes-copy.txt notes.txt recipes.txtIf the target is a folder, the copy keeps its name and lands in that folder.
Copying folders: cp -r
For a folder, plain cp refuses with -r not specified; omitting directory. With -r (recursive) it copies the folder
with everything inside:
cp -r projects/website projects/website-backup
Purpose: Copies a folder with all files and folders inside it.
Syntax: cp -r FOLDER TARGET
Example:
cp -r projects/website projects/website-backup
ls projects
website website-backupMoving: mv
mv works like cp, but the original disappears from the old place. Moving the weekend photo into Pictures:
mv Downloads/photo.jpg Pictures/
Purpose: Moves a file or folder to another place.
Syntax: mv [OPTION]... SOURCE TARGET
Options:
Example:
mv Downloads/photo.jpg Pictures/
ls Pictures
cat.jpg photo.jpgThe slash at the end of Pictures/ is a small safety net: it tells mv that the target must be an existing folder.
Without it, a typo like Picture would quietly rename the photo to a file called Picture.
Renaming is moving
To rename a file, you also use mv. You move the file to a new name in the same folder:
mv Downloads/invoice.pdf Downloads/invoice-october.pdf
Purpose: Renames a file by moving it to a new name in the same folder.
Syntax: mv OLD-NAME NEW-NAME
Example:
mv Downloads/invoice.pdf Downloads/invoice-october.pdf
ls Downloads
invoice-october.pdf photo.jpgPractice
Terminal task 1/3
Move photo.jpg from Downloads into the folder Pictures.
Task 1/3: Move photo.jpg from Downloads into the folder Pictures.
Move photo.jpg from Downloads into the folder Pictures.
Task complete
Solution
mv Downloads/photo.jpg Pictures/ Why: The photo is then in one place only. With cp you would have two copies and would have to tidy up again.
Another way
ls Pictures
mv -i Downloads/photo.jpg Pictures/ Looking first and adding -i costs a few seconds and protects a photo with the same name in the target.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 2/3
Make a backup of the folder projects/website with everything in it. The copy should be called projects/website-backup.
Task 2/3: Make a backup of the folder projects/website with everything in it. The copy should be called projects/website-backup.
Make a backup of the folder projects/website with everything in it. The copy should be called projects/website-backup.
Task complete
Solution
cp -r projects/website projects/website-backup Why: A backup before a change is your undo button. If something goes wrong, the old state is still there.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 3/3
Copy notes.txt from your home folder into Documents. An older notes.txt already lies there and must stay unchanged, so give the copy the name notes-new.txt.
Task 3/3: Copy notes.txt from your home folder into Documents. An older notes.txt already lies there and must stay unchanged, so give the copy the name notes-new.txt.
Copy notes.txt from your home folder into Documents. An older notes.txt already lies there and must stay unchanged, so give the copy the name notes-new.txt.
Task complete
Solution
cp notes.txt Documents/notes-new.txt Why: Copying straight into Documents/ would have replaced the older notes without a word. A new name keeps both.
Another way
ls Documents
cp -i notes.txt Documents/notes-new.txt First ls shows that notes-new.txt is still free, and -i would ask if it were not. Two safety nets for one line more.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Fill the gap
To copy a folder with everything in it, cp needs the option .
To copy a folder with everything in it, cp needs the option ….
Check works once you have typed an answer.
Find the error
This command answers "Not a directory" and moves nothing. Tap the part that is wrong.
Parts of the command
Check works once you have tapped a part.
Try cp -i notes.txt Documents/ in the terminal above and answer n: the old notes stay as they were. That brings
you halfway through Terminal start. Next up: deleting without regrets.
Quiz
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works when exactly 2 are chosen. Chosen: 0.
Cards
-
Card 1 of 4
Tap the card or press Space to turn it over.
-
Card 2 of 4
Tap the card or press Space to turn it over.
-
Card 3 of 4
Tap the card or press Space to turn it over.
-
Card 4 of 4
Tap the card or press Space to turn it over.
End of the lesson
Still open: 5 exercises, 4 quiz questions
What you can do now: cp notes.txt Documents/notes-copy.txt, cp -r projects/website projects/website-backup, mv Downloads/photo.jpg Pictures/, mv Downloads/invoice.pdf Downloads/invoice-october.pdf
+0 XP in this lesson
- Lesson 1: open
- Lesson 2: open
- Lesson 3: open
- Lesson 4: open
- Lesson 5: open
- Lesson 6: done
- Lesson 7: open
- Lesson 8: open
- Lesson 9: open
- Lesson 10: open
- Lesson 11: open
- Lesson 12: open
Path complete
You finished all 12 lessons of Terminal start.
On to Linux Basics: lesson 1, Finding your way around a strange server (8 min) Back to the path
Cheat sheet
-
cp <source> <target>copy a file -
cp -r <folder> <target>copy a folder with its content -
mv <source> <folder>/move into a folder -
mv <old> <new>rename -
cp -i, mv -iask before overwriting