Basic Linux Commands: The ln Command

Page content

A link is basically a pointer that links two files - one to the other. This gets a bit confusing because one of those endpoints isn’t really a file, it’s a link. For example: You have a file in /data called reports which is a large database that you want all of your users to be able to use. Instead of having to copy that file to each users’ home directory, and then go through the trouble of making sure each user updates the original file when they are finished working, you would create a link from their home directories to the reports file. With each user having a symbolic link to the same file, when they update the file it will be reflected in the original. Of course this can cause problems if more than one user is working on the file at a time and one save can overwrite anothers’ work.

There are two kinds of links: Hard and soft (or sym) links. They are very similar to one another, but the variations make them quite different. Here are the specifications for each:

Hard Link:

  • Is a pointer to a file or program but NOT a directory.
  • If the linked file is moved or destroyed, the link is not broken.
  • Hard links can NOT span from one drive to another.

Soft Link:

  • Is a pointer to a file, program, or directory.
  • If the linked file is moved or destroyed, the link is broken.
  • Soft links CAN span from one drive to another.

Creating links is done with the ln command. Say you want to create a link to the reports database on the /data directory. To create a hard link to this directory, enter the command (from within the directory you want the link to be):

ln /data/reports

Now, let’s suppose that database is on a different drive. For this you need to create a soft link (or symlink) like so:

ln -s /data/reports

Remove links with the standard Linux rm command like:

rm ~/reports

The above command would remove only the link created in your home directory. Any time you use that file, when you save data, the saved data would be immediately reflected in the original file that you linked to.

Final Thoughts

Although they seem complex, links are actually quite easy to use. The most complex issue is knowing when to use what type of link. Once you have that mastered, you’ll be using links like a pro.

This post is part of the series: Linux Command Line

If you ever plan on doing any administration on a Linux machine, you would be well served to get to know the command line interface. In this Bright Hub series you will be introduced to various concepts surrounding one of the most powerful admin tools around.

  1. Linux Command Line: Introduction
  2. Linux Command Line: ls
  3. Linux Command Line: cd
  4. Linux Command Line: mkdir
  5. Linux Command Line: df
  6. Linux Command Line: ln
  7. Linux Command Line: top
  8. Linux Command Line: mount/umount
  9. Linux Command Line: Cron/Crontab
  10. Linux Command Line: chmod
  11. Linux Command Line: wget
  12. Linux Command Line: cat
  13. Linux Command Line: grep
  14. Linux Command Line: dd
  15. Linux Command Line: sudo
  16. Linux Command Line: startx
  17. Linux Command Line: adduser
  18. Linux Command Line: at
  19. Linux Command Line: aterm
  20. Linux Command Line: nano
  21. Linux Command Line: hostname