How to create symbolic links and hard links in Linux/Unix

The hard links and the symbolic links, also known as symlinks or soft links are the link files in Unix and Linux systems. A link points to a file and when clicked, it will open the targeted file.

The symlink gets a different inode than the targeted file, while the hardlink gets the same.

When you change the owner of a symlink, you actually change the owner of the targeted file. Also, the symlink’s size is equal (in bytes) with the number of characters in the targeted filename.

The hard links and the sym links are both used for giving multiple names to a single file.

The main differences between the two types of links are:

The symlinks can point to directoriers and the hard links cannot
When the targeted file is moved, the symlink will be unusable, but the hardlink will update itself.

$ ls -l /sbin/halt
lrwxrwxrwx 1 root root 6 2012-05-21 22:38 /sbin/halt -> reboot

How to create symlinks:

Syntax: ln -s target_name symlink_name

$ ln -s ~/work/naboo/script myscript
$ ls -l myscript
lrwxrwxrwx 1 razvan razvan 30 2012-06-10 16:04 myscript -> /home/razvan/work/naboo/script

How to create hard links:

Syntax: ln target_name link_name

$ ln ~/work/naboo/script myscript2
$ ls -l myscript2
-rw-rw-r-- 2 razvan razvan 0 2012-06-10 16:04 myscript2

You can find the other link related articles here.

Share and Enjoy

  • Twitter
  • Facebook
  • Google Plus
  • Email
  • RSS
Tagged with: , , , , , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*