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?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

System Admin Handbook

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

This book approaches system administration in a practical way and is an invaluable reference for both new administrators and experienced professionals. It details best practices for every facet of system administration, including storage management, network design and administration, email, web hosting, scripting, and much more.

$ Check price
74.9974.99Amazon 4.5 logo(142+ reviews)

More Linux & UNIX resources