terminal.cab

Terminal start Lesson 6 of 12

guestLevel 1Badges and progress0/100 XP, XP to the next levelStreak0

Copying, moving and renaming

10 minutes

Learning goals

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:

  • -r copy folders with everything in them
  • -i ask before overwriting an existing file

Example:

cp notes.txt Documents/notes-copy.txt
ls Documents
'Holiday 2027.txt'   letter.txt   notes-copy.txt   notes.txt   recipes.txt

in the glossary: cp

If 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-backup

in the glossary: cp

Moving: 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:

  • -i ask before overwriting an existing file

Example:

mv Downloads/photo.jpg Pictures/
ls Pictures
cat.jpg  photo.jpg

in the glossary: mv

The 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.jpg

in the glossary: mv

Practice

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.

Loading terminal …

lerner@terminal: ~bash
lerner@terminal:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@terminal: ~bash
lerner@terminal:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@terminal: ~bash
lerner@terminal:~$

Esc, then Tab leaves the terminal

Not done yet

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

  1. Question 1 of 4How do you rename invoice.pdf to invoice-october.pdf?

    Check works once you have chosen an answer.

  2. Question 2 of 4What does cp projects/website backup do without -r?

    Check works once you have chosen an answer.

  3. Question 3 of 4Documents already contains notes.txt. What happens with mv notes.txt Documents/?

    Check works once you have chosen an answer.

  4. Question 4 of 4You want to copy notes.txt into Documents, where a notes.txt may already exist. Which steps protect the old file? Choose TWO.

    Check works when exactly 2 are chosen. Chosen: 0.

Cards

  1. Card 1 of 4

    Tap the card or press Space to turn it over.

  2. Card 2 of 4

    Tap the card or press Space to turn it over.

  3. Card 3 of 4

    Tap the card or press Space to turn it over.

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

Next: lesson 7, Deleting without regrets (10 min)

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

  1. Lesson 1: open
  2. Lesson 2: open
  3. Lesson 3: open
  4. Lesson 4: open
  5. Lesson 5: open
  6. Lesson 6: done
  7. Lesson 7: open
  8. Lesson 8: open
  9. Lesson 9: open
  10. Lesson 10: open
  11. Lesson 11: open
  12. Lesson 12: open
1 of 12

Next: lesson 7, Deleting without regrets (10 min) That's enough for today

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