Linux Basics Lesson 6 of 38
Delete, link, measure
10 minutes
Learning goals
- Delete with rm and rm -r, and check first with ls what a pattern hits.
- Explain hard links and symbolic links, and read the link count in ls -l.
- Recognise a broken symbolic link and repair the cause.
- Find out with du how much space folders take up.
Delete: rm
You know rm from Terminal-Start, lesson 7. On a server the same rule applies, only the
price of a mistake is higher: first show with ls what the pattern hits, then delete with exactly the same pattern.
rm projects/shop/build.log
Purpose: Deletes for good. There is no trash can.
Syntax: rm [OPTION]... FILE...
Options:
Example:
ls projects/shop/*.log
projects/shop/build.log projects/shop/test.log
rm projects/shop/*.log
ls projects/shop
README.md app.conf releasesrm -r takes folders along with everything in them, rm -i asks before each file. rm -f never asks and stays silent
about files that do not exist.
Two kinds of links
ln without an option creates a hard link, a second name for the same data. Which data a name belongs to is decided by
the inode, the number the file system uses internally. Your first look at it is the second column of ls -l, the link
count. It says how many names a file has.
A symbolic link, ln -s, is a small file of its own that only contains a path. In ls -l it starts with an l and
shows its target behind an arrow. Note the order: first the real target, then the name of the link, just like cp.
ln -s projects/shop/releases/v2.4.1 current
Purpose: Creates a link. With -s a signpost to a path, without -s a second name.
Syntax: ln [-s] TARGET LINKNAME
Options:
Example:
ln -s projects/shop/releases/v2.4.1 current
ls -l current
lrwxrwxrwx 1 lerner lerner 29 Oct 17 09:42 current -> projects/shop/releases/v2.4.1
cat current/VERSION
2.4.1
ln notes.txt second.txt
stat -c '%h links, inode %i: %n' notes.txt second.txt
2 links, inode 78: notes.txt
2 links, inode 78: second.txtBoth names of the hard link show the same inode and the count 2. Delete one, and the data is still there under the other. With a symbolic link it is different: delete the target, and the link stays behind, broken.
ln -s notes.txt pointer
readlink pointer
notes.txt
rm notes.txt
cat pointer
cat: pointer: No such file or directory
cat complains about pointer, though pointer exists. What is missing is its target. readlink shows where a link points
without following it. That is the first thing to check when a file "that is definitely there" cannot be found.
How much space does it take?
du adds up what files and folders occupy. -s gives one total per argument, -h makes it readable. df answers a
different question: how full is the whole disk? There is more about it in module 8.
du -sh *
Purpose: Shows how much space files and folders take up.
Syntax: du [-sh] [PATH]...
Options:
Example:
du -sh *
8.0K Documents
95M archive
12K logs
4.0K notes.txt
44K projects
df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 32G 4.3G 28G 14% /On a real server you would add | sort -rh | head to get the biggest first: du -sh * | sort -rh | head. sort -h
understands sizes like 95M and 4.0K. This lesson does not load it, so here you read the list yourself.
Practice
Terminal task 1/4
Delete all .log files in projects/shop with one pattern. Check first with ls what the pattern hits. Everything else stays.
Task 1/4: Delete all .log files in projects/shop with one pattern. Check first with ls what the pattern hits. Everything else stays.
Delete all .log files in projects/shop with one pattern. Check first with ls what the pattern hits. Everything else stays.
Task complete
Solution
ls projects/shop/*.log
rm projects/shop/*.log Why: The shell expands the pattern before ls or rm even start, so both get the same list. 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 2/4
Create a symbolic link called current in your home folder that points to projects/shop/releases/v2.4.1.
Task 2/4: Create a symbolic link called current in your home folder that points to projects/shop/releases/v2.4.1.
Create a symbolic link called current in your home folder that points to projects/shop/releases/v2.4.1.
Task complete
Solution
ln -s projects/shop/releases/v2.4.1 current Why: A rollback is now only a new link to v2.4.0. Nothing gets copied, and the service always finds its files under the same name.
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/4
Give notes.txt a second name second.txt with a hard link, then show the link count and name of both with stat -c '%h %n'.
Task 3/4: Give notes.txt a second name second.txt with a hard link, then show the link count and name of both with stat -c '%h %n'.
Give notes.txt a second name second.txt with a hard link, then show the link count and name of both with stat -c '%h %n'.
Task complete
Solution
ln notes.txt second.txt
stat -c '%h %n' notes.txt second.txt Why: The count went from 1 to 2. Delete notes.txt now and it drops back to 1, and the text is still there under second.txt. The space is only free when the count reaches 0.
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 4/4
Find out how much space each entry in your home folder takes up, one total each.
Task 4/4: Find out how much space each entry in your home folder takes up, one total each.
Find out how much space each entry in your home folder takes up, one total each.
Task complete
Solution
du -sh * Why: The biggest one stands out at once: the archive, not the logs. Note that * leaves out names with a leading dot, so hidden folders do not show up here.
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
ln notes.txt b.txt && stat -c %h b.txtBefore the command, in your home folder ~
- ~/notes.txt
Check works once you have chosen an answer.
Find the error
This line should create a link called current that points to releases/v2.4.1. Tap the part that is in the wrong place.
Parts of the command
Check works once you have tapped a part.
Next up: finding files by name, age and size.
Quiz
-
Check works once you have chosen an answer.
-
Check works when exactly 2 are chosen. Chosen: 0.
-
Check works once you have chosen an answer.
Cards
-
Card 1 of 3
Tap the card or press Space to turn it over.
-
Card 2 of 3
Tap the card or press Space to turn it over.
-
Card 3 of 3
Tap the card or press Space to turn it over.
End of the lesson
Still open: 6 exercises, 3 quiz questions
Next: lesson 7, Finding files by name, age and size (10 min)
What you can do now: rm projects/shop/build.log, ln -s projects/shop/releases/v2.4.1 current, du -sh *
+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
- Lesson 13: open
- Lesson 14: open
- Lesson 15: open
- Lesson 16: open
- Lesson 17: open
- Lesson 18: open
- Lesson 19: open
- Lesson 20: open
- Lesson 21: open
- Lesson 22: open
- Lesson 23: open
- Lesson 24: open
- Lesson 25: open
- Lesson 26: open
- Lesson 27: open
- Lesson 28: open
- Lesson 29: open
- Lesson 30: open
- Lesson 31: open
- Lesson 32: open
- Lesson 33: open
- Lesson 34: open
- Lesson 35: open
- Lesson 36: open
- Lesson 37: open
- Lesson 38: open
Cheat sheet
-
rm -r <folder>delete a folder with its content, first ls -
ln -s <target> <name>symbolic link, the signpost -
ln <file> <name>hard link, a second name -
readlink <link>where does this link point -
du -sh *size of each entry here
The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.
- Download PDF (PDF, 19 KB)
- View as image (PNG, 63 KB)