terminal.cab

Linux Basics Lesson 6 of 38

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

Delete, link, measure

10 minutes

Learning goals

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:

  • -r delete folders with everything in them
  • -i ask before each file
  • -f never ask, and be silent about missing files

Example:

ls projects/shop/*.log
projects/shop/build.log  projects/shop/test.log
rm projects/shop/*.log
ls projects/shop
README.md  app.conf  releases

in the glossary: rm

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

  • -s symbolic link, also for folders and across file systems

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

in the glossary: ln

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

  • -s one total per argument, not every subfolder
  • -h sizes like 95M
  • -a also list single files

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% /

in the glossary: du

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

Predict the output

Command

ln notes.txt b.txt && stat -c %h b.txt

Before the command, in your home folder ~

  • ~/notes.txt
What does ln notes.txt b.txt && stat -c %h b.txt print?

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

  1. Question 1 of 3You delete one of the two names of a hard link. What happens to the data?

    Check works once you have chosen an answer.

  2. Question 2 of 3Where do you need a symbolic link, because a hard link would not work? Choose TWO.
  3. Question 3 of 3What is the difference between du and df?

    Check works once you have chosen an answer.

Cards

  1. Card 1 of 3

    Tap the card or press Space to turn it over.

  2. Card 2 of 3

    Tap the card or press Space to turn it over.

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

  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
  13. Lesson 13: open
  14. Lesson 14: open
  15. Lesson 15: open
  16. Lesson 16: open
  17. Lesson 17: open
  18. Lesson 18: open
  19. Lesson 19: open
  20. Lesson 20: open
  21. Lesson 21: open
  22. Lesson 22: open
  23. Lesson 23: open
  24. Lesson 24: open
  25. Lesson 25: open
  26. Lesson 26: open
  27. Lesson 27: open
  28. Lesson 28: open
  29. Lesson 29: open
  30. Lesson 30: open
  31. Lesson 31: open
  32. Lesson 32: open
  33. Lesson 33: open
  34. Lesson 34: open
  35. Lesson 35: open
  36. Lesson 36: open
  37. Lesson 37: open
  38. Lesson 38: open
1 of 38

Next: lesson 7, Finding files by name, age and size (10 min) That's enough for today

Path complete

You finished all 38 lessons of Linux Basics.

Back to the path

Cheat sheet

The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.