Terminal start Lesson 7 of 12
Deleting without regrets
10 minutes
Learning goals
- Look at the matches with ls and the same pattern before you delete them with rm.
- Delete files with rm, folders with rm -r, empty folders with rmdir.
- Let rm -i ask before every file, and know that the terminal has no trash can.
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.pdfIf 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:
Example:
rm Downloads/invoice.pdf
ls Downloads
photo.jpgrm 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 projectsFor 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 emptyPractice
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.
Task complete
Solution
cat Documents/notes.txt
rm Documents/notes.txt Why: One look costs a second. Without it you might delete the wrong notes, and rm has no undo.
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
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.
Task complete
Solution
ls Downloads/*.jpg
rm Downloads/*.jpg Why: ls and rm see exactly the same list for the same pattern. If ls shows the right files, rm deletes the right files.
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
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.
Task complete
Solution
ls projects/website
rm -r projects/website Why: rm -r takes everything inside without asking. A quick ls first shows you what that is.
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.
Predict the output
Command
rm DocumentsBefore the command, in your home folder ~
- ~/Documents/
- ~/Documents/Holiday 2027.txt
- ~/Documents/letter.txt
- ~/Documents/notes.txt
- ~/Documents/recipes.txt
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
-
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: 6 exercises, 4 quiz questions
What you can do now: ls Downloads/*.pdf, rm Downloads/invoice.pdf, rm -r projects/website, rmdir old
+0 XP in this lesson
- Lesson 1: open
- Lesson 2: open
- Lesson 3: open
- Lesson 4: open
- Lesson 5: open
- Lesson 6: open
- Lesson 7: done
- 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
-
ls <pattern>check what a pattern matches -
rm <file>delete a file, no trash can -
rm -i <file>ask before every file -
rm -r <folder>delete a folder with its content -
rmdir <folder>delete an empty folder