🎓 A1Learner
🔍
0 of 50 lessons completed

Links

A link is a second name for a file. One file can then be reached by two names.

A second name

The link is a second name for the file.

$ cd ~/practice
$ echo "Practice notes" > link-target.txt
$ ln -s link-target.txt my-link
$ ls -l my-link link-target.txt

Two names, one file

The hard link names the same file, so the count reads 2.

$ cd ~/practice
$ ln link-target.txt hard-link
$ ls -l link-target.txt hard-link

Read the link

readlink prints the name the link points to. cat follows the link and prints the file.

$ cd ~/practice
$ readlink my-link
$ cat my-link

Example

$ cd ~/practice
$ echo "Shared notes" > shared.txt
$ ln -s shared.txt shared-link
$ ln shared.txt shared-hard
$ ls -l shared-link shared.txt shared-hard
$ readlink shared-link

Key points

  • A symbolic link points to a file name.
  • A hard link names the same file itself.
  • The target is the file a link points to.
  • Use ln -s to make a symbolic link.
  • Use ln to make a hard link.
  • ls -l marks a symbolic link with an arrow.
  • Use readlink to print the target of a symbolic link.
  • Opening a link opens the file it points to.

In this example, shared.txt gets two extra names. The symbolic link points to shared.txt. The hard link names the same file, so the count reads 2. readlink prints the name the symbolic link points to. The names and dates here come from this machine, so yours will differ.

Note

Removing the file breaks a symbolic link that points to it.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does ln -s make?