11. Creating Hard and Symbolic Links ln, -s

Links are very similar to shortcuts, which many of you may be familiar with. They allow files to have multiple identities across the filesystem.

There are two types of links - hard and symbolic links.

What is a hard link?

A hard link is an additional name for an existing file.

  • Hard links are not a pointer to a file, but a directory entry pointing to the same inode. (An inode is a data structure that contains information about the file.)
  • Essentially, it's identical to the original file.
  • If you change the name of the file, the hardlink will still point to it.
  • However, if you replace the file with a new version, the hard link will no longer point to it.
  • To delete the file, you have to delete all hard links pointing to it.

Limitations to hard links

  • Cannot span physical file systems.
  • Cannot reference directories, only files.

To create a hardlink, simply use the ln command.

Let's demonstrate this by an example.

$ echo 'Testing' > sample.txt
$ ln sample.txt sampleHardLink.txt
$ cat sampleHardLink.txt

The first line simply takes the word Testing and outputs it to a newly created file called sample.txt. The second line creates the hard link, and the third line uses the more command to output the file contents of sampleHardLink.txt. We'll learn about the echo command and redirections (>) shortly.

Now if I go edit sampleHardLink.txt, the changes will also show up in my sample.txt file. Note that I can place the hard link anywhere I'd like on my file system.

What is a symbolic link?

A symbolic link is actually very closely related to hard links, with added improvements.

  • These actually point to the file through its path file name.
  • It resolves the name of the file every time you access it.
  • If you move the file, the symlink does not follow.
  • However, if you update the file with a new version of the same name, it'll still work since the path will be the same.

Symbolic links were made to overcome the limitations of hard links

  • Able to span physical devices.
  • Can reference directories

To create a symbolic link, use the -s option:

$ ln -s targetFileOrDir linkName   

Try experimenting with hard and soft links own your own to get a feel for their differences and features.

Removing existing links

To remove any existing links or files associated with a link, use the -f option.

Aching back from coding all day?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

Command Line Kung Fu

Take your Linux skills to the next level! Try Linux & UNIX

Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.

$ Check price
14.9914.99Amazon 4.5 logo(27+ reviews)

More Linux & UNIX resources