The filesystem and archives

INPUT · Slides

Make a shortcut (`ln -s`)

01 / 07

A note saying "look over there"

Look again at what you saw last lesson.

~ $ ls -l /bin/lslrwxrwxrwx  1 root root  7  /bin/ls -> busybox

What is inside /bin/ls was not the ls program. It is a note saying "look at busybox".

That is called a symbolic link. "Symbolic" as in "made of a symbol", and it is often shortened to symlink.

Here is how you make one.

~ $ ln -s origin.txt link.txt~ $ cat link.txthello              <- the contents of origin.txt come out

ln is short for link, and the -s is the s of symbolic.

Watch the order.

ln -s what-it-points-at the-name-to-make      ^                 ^   the real thing     the note (the new one)

Think of it as the same order as cp: the original, then the new name.

02 / 07

It holds no contents

A link holds no contents. All it holds is the text of what it points at.

~ $ readlink link.txtorigin.txt

readlink is the command that tells you what is written on the note.

So this is what happens.

remove the link      -> the real thing is fine (you threw away a note)remove the real one  -> the link stays, and is lost

The second matters. The link sits there quite unbothered when what it points at has gone.

~ $ rm origin.txt~ $ cat link.txtcat: can't open 'link.txt': No such file or directory

link.txt is right there and you are told it is not. The note exists, and there is nothing where it leads. That is called a dangling link.

It looks no different in ls, which is what makes it hard to spot.

03 / 07

It can be a shortcut to a directory too

What a link points at need not be a file. It can be a directory.

~ $ ln -s deep/inner shortcut~ $ ls shortcutdata.txt~ $ cat shortcut/data.txtinner

A shortcut to somewhere deep.

deep/inner/data.txt      the proper wayshortcut/data.txt        the short way

When a place you visit daily sits deep down, one shortcut at the door of your room makes life easier.

You can cd into it too.

~ $ cd shortcut~/shortcut $ pwd/home/learner/shortcut     <- the way you came~/shortcut $ pwd -P/home/learner/deep/inner   <- where you really are

pwd shows "the way you came" and -P (physical) shows "where you really are". When you forget you took the shortcut, pwd -P settles it.

04 / 07

Point at it relatively, or absolutely

This is where mistakes come from. There are two ways to write what it points at.

ln -s origin.txt link.txt                    relative (the origin.txt in the same place)ln -s /home/learner/origin.txt link.txt      absolute (the origin.txt at this path)

A link written relatively remembers the way there as seen from where the link sits. So move the link and it breaks.

~ $ mv link.txt box/~ $ cat box/link.txtcat: can't open ... No such file or directory

There is no origin.txt inside box. The note says "look at the origin.txt in the same place as me", so moving house changed what it points at.

Here is how to choose.

How it is writtenWhen it suits
relativewhen the link and the real thing move together
absolutewhen the link alone might move

When in doubt, absolute is safer. It is longer, and it does not break when moved.

05 / 07

Repoint it with -f

What is really nice about links is that you can repoint them.

~ $ ln -s v1.txt now.txt      now.txt points at v1~ $ ln -sf v2.txt now.txt     repointed, now at v2

-f (force) means "overwrite even if that name is already there". Without it you are told File exists.

This shape comes up constantly in the field.

app-1.0.0/     the old versionapp-1.1.0/     the new versionapp -> app-1.1.0    <- points at the version in use

Whoever uses it always writes app. To swap the contents you only repoint the link. If the new version gives trouble, put the link back and you are back where you were.

swapping over takes an instantgoing back takes an instant

No copying gigabytes again. The idea is "change only the name".

06 / 07

Finding links, and following them

Here are the tools together.

ln -s target name     make oneln -sf target name    repoint onereadlink name         see what is written on itreadlink -f name      see the real place at the end of the trailfind . -type l        list only the linksrm name               remove the link (the real thing is fine)

The two shapes of readlink, side by side.

~ $ readlink /bin/lsbusybox              <- exactly as written~ $ readlink -f /bin/cat/bin/busybox         <- the absolute path at the end of the trail

find . -type l is for tidying up. -type f (ordinary files) and -type d (directories) came in chapter 3; add l and only links come out.

One warning when removing.

rm link            removes the link (right)rm link/           with a link to a directory this can go wrong

Do not put a / on the end; that is the safe way. With it you end up pointing at "the contents".

07 / 07

Now have a go

Here is a picture of what you are making.

origin.txt      the real thing, saying hellolink.txt --->   the note saying "look at origin.txt"

There are only three things to remember.

1. make it with ln -s (the original, then the new name)2. it holds no contents (lose the target and it is lost)3. repoint it with ln -sf

In the practice you make a dangling link on purpose. Seeing the shape that does not work first means that when you meet "it keeps saying the file is not there" out in the world, you will spot it at once.

Next lesson brings the other kind of link (a hard link). That one is not a note but a second name on the same contents. Compared with today's, the difference stands out.