terminal.cab

Terminal start Lesson 7 of 12

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

Deleting without regrets

10 minutes

Learning goals

No trash can

rm (remove) deletes files right away. There is no trash can and no undo, not even for experienced people. That is why this lesson starts with a habit, not with a command.

First look, then delete

Before you delete with a pattern like *.pdf, run ls with exactly the same pattern. ls only shows what the pattern matches and changes nothing:

ls Downloads/*.pdf

Purpose: Shows which files a pattern matches, without touching them.

Syntax: ls PATTERN

Example:

ls Downloads/*.pdf
Downloads/invoice.pdf

in the glossary: ls

If the list is what you expect, press the up arrow, replace ls with rm and press Enter. Now the very same files go away, not one more:

rm Downloads/invoice.pdf

Purpose: Deletes files for good. There is no trash can.

Syntax: rm [OPTION]... FILE...

Options:

  • -i ask before every file
  • -r delete folders with everything in them

Example:

rm Downloads/invoice.pdf
ls Downloads
photo.jpg

in the glossary: rm

rm says nothing when it worked. No news is good news on the command line.

Deleting folders

For a folder, plain rm refuses with Is a directory. With -r (recursive) it deletes the folder and everything inside, without asking:

rm -r projects/website

Purpose: Deletes a folder with all files and folders inside it.

Syntax: rm -r FOLDER

Example:

ls projects/website
index.html  server.log  style.css
rm -r projects/website
ls projects

in the glossary: rm

For an empty folder there is a careful tool: rmdir only deletes a folder that is empty. If something is still inside, it refuses, and nothing is lost.

rmdir old

Purpose: Deletes a folder, but only if it is empty.

Syntax: rmdir FOLDER

Example:

mkdir old
rmdir old
rmdir Documents
rmdir: failed to remove 'Documents': Directory not empty

in the glossary: rmdir

Practice

Terminal task 1/3

Documents/notes.txt is an old copy of your notes with just one line. Look at it, then delete it. Everything else stays.

Task 1/3: Documents/notes.txt is an old copy of your notes with just one line. Look at it, then delete it. Everything else stays.

Documents/notes.txt is an old copy of your notes with just one line. Look at it, then delete it. Everything else stays.

Loading terminal …

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

Esc, then Tab leaves the terminal

Not done yet

Terminal task 2/3

Delete every .jpg file in Downloads with one pattern. Check first with ls which files the pattern matches. The PDF and the pictures in Pictures stay.

Task 2/3: Delete every .jpg file in Downloads with one pattern. Check first with ls which files the pattern matches. The PDF and the pictures in Pictures stay.

Delete every .jpg file in Downloads with one pattern. Check first with ls which files the pattern matches. The PDF and the pictures in Pictures stay.

Loading terminal …

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

Esc, then Tab leaves the terminal

Not done yet

Terminal task 3/3

The old website in projects/website is no longer needed. Delete the folder with everything in it. The folder projects itself stays.

Task 3/3: The old website in projects/website is no longer needed. Delete the folder with everything in it. The folder projects itself stays.

The old website in projects/website is no longer needed. Delete the folder with everything in it. The folder projects itself stays.

Loading terminal …

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

Esc, then Tab leaves the terminal

Not done yet

Predict the output

Command

rm Documents

Before the command, in your home folder ~

  • ~/Documents/
  • ~/Documents/Holiday 2027.txt
  • ~/Documents/letter.txt
  • ~/Documents/notes.txt
  • ~/Documents/recipes.txt
What does rm Documents print?

Check works once you have chosen an answer.

Find the error

This command was meant to delete the PDFs in Downloads. It deletes far more. Tap the part that is wrong.

Parts of the command

Check works once you have tapped a part.

Fill the gap

rmdir only deletes a folder if it is .

rmdir only deletes a folder if it is ….

Check works once you have typed an answer.

Try rm -i notes.txt in the terminal above and answer n: the notes stay. Next up: where to find help when you are stuck.

Quiz

  1. Question 1 of 4You deleted a file with rm by mistake. Where do you find it?

    Check works once you have chosen an answer.

  2. Question 2 of 4Which command deletes the folder old with all files in it?

    Check works once you have chosen an answer.

  3. Question 3 of 4You want to delete all .log files in projects/website. What is the safe first step?

    Check works once you have chosen an answer.

  4. Question 4 of 4Which commands ask before they delete or overwrite something? 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: 6 exercises, 4 quiz questions

Next: lesson 8, Getting help (10 min)

What you can do now: ls Downloads/*.pdf, rm Downloads/invoice.pdf, rm -r projects/website, rmdir old

+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: open
  7. Lesson 7: done
  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 8, Getting help (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