Everything about the Symlinks and Hardlinks On Linux

Symlinks and Hardlinks

Files are arranged in directories and each file can be reached through a series of directories and subdirectories from the root - correct? Yes ... BUT ... there are sometimes that the same file can be reached through several names, and on Unix and Linux systems this is known as a "link".

In this article, We are going to understand some important points and working techniques of Symlinks and Hardlinks.

 There are two ways a link can be set up.

1. Symlink
2. Hardlink

1. Symlink [Symbolic Link]:

A symlink is also called as Soft link.  Symlink is a file which contains a reference to another file or directory in the form of absolute or relative path.

In simple word, you can create a shortcut of file or directory to the other path using symlink feature.

Important points about the Symlink:

$ Links have different inode numbers.

Actual file:

54665305 -rw-r--r--  1 prashant prashant     0 Nov 15 14:56 testsymlink

Symlink of file:

55057538 lrwxrwxrwx 1 prashant prashant 31 Nov 15 14:57 testsymlink -> /home/prashant/test/testsymlink

The Red mark nos. are the inode no of that file

$ Removing symlink will not affect the original file but if you remove original file then symlink will not work. the symlink will be changed to red color once original file removed

lrwxrwxrwx  1 prashant prashant    31 Nov 15 14:5te7 testsymlink -> /home/prashant/test/testsymlink

$ Symbolic links are set up using the ln command with the -s option.
$ Symlink is slightly slower at runtime compared to hard link but it's more flexible and more often used in day to day admin work.
$ Symlink always created with the 777 permission.
$ Symlink can create directories

Some Practical Examples of Symlink:

1. Create symlink for abc as xyz under the from "/opt/new-file" to "/home/prashant" directory.

- cd /opt/new-file/
- vi abc
##Write some content to this file and then save the file.

- cd /home/prashant
- ln -s /opt/new-file/abc xyz
- ls -l xyz
- xyz -> /opt/new-file/abc [symlink]
- cat xyz [You will see the content from abc file]

2. Create symlink to the directory from one path to another.

- cd /opt/new-file/
- mkdir testdir
- touch file1, file2, file3
- cd /home/prashant/
- ln -s /opt/new-file/testdir
- ls -l testdir
- lrwxrwxrwx 1 root root 21 Nov 15 15:32 testdir -> /opt/new-file/testdir/

You can point that directory with another name run below command,

- ln -s /opt/new-file/testdir symdir
- ls -l symdir
- lrwxrwxrwx 1 root root 21 Nov 15 15:32 symdir -> /opt/new-file/testdir

Hardlink :

A HardLink is where a file has two names which are both on an equal weighting, and both of the file names in the "inode table" point directly to the blocks on the disc that contain the data.

Important points for Hardlink :

$ All Links have same inode number.

- ln abc xyz
- ll -i
- 37626170 -rw-r--r-- 2 root root 31 Nov 15 15:22 abc
- 37626170 -rw-r--r-- 2 root root 31 Nov 15 15:22 xyz

In above example, you wil see both files inode nos. are same.

$ Links have actual file contents.
$ Removing any link just reduces the link count but doesn't affect other links.
$ Hard links can not cross partition which means you can not create hard link of one file to another partition.
$ Hardlinks can not create directories.
$ Hardlink can be create using ln command without any option.

Practicle Example of Hardlink:

1. Now you create a hard link as follows:

$ touch file1
$ ln file1 file2
$ ls -l

Output:
-rw-r--r-- 2 sadhiq sadhiq 0 2006-01-30 13:28 file1
-rw-r--r-- 2 sadhiq sadhiq 0 2006-01-30 13:28 file2

Now just see inode of both file1 and file2:

$ ls -i file1
782263
$ ls -i file2
782263

As you can see inode number is same for hard link file called file2 in the inode.

2. Try creating hardlink with directories it will give you the error like "hard link not allowed for directory".

3. Try creating hardlink to another partition it will produce an error like " failed to create hard link ‘/boot/qwe’ => ‘/opt/new-file/abc’: Invalid cross-device link".

Unlink Command:

This command is used to unlink or remove the link from files.

Syntax: unlink /path/to/symlinkfile

Some more option you can use with the ln command.

-d, -F, --directory allow the superuser to attempt to hard link
directories (note: will probably fail due to
system restrictions, even for the superuser)
-f, --force remove existing destination files
-i, --interactive prompt whether to remove destinations
-L, --logical dereference TARGETs that are symbolic links
-n, --no-dereference treat LINK_NAME as a normal file if
it is a symbolic link to a directory
-P, --physical make hard links directly to symbolic links
-r, --relative create symbolic links relative to link location
-s, --symbolic make symbolic links instead of hard links
-S, --suffix=SUFFIX override the usual backup suffix
-t, --target-directory=DIRECTORY specify the DIRECTORY in which to create
the links
-T, --no-target-directory treat LINK_NAME as a normal file always
-v, --verbose print name of each linked file
--help display this help and exit
--version output version information and exit

This is all about the symlink and hardlink.  I hope you like the article if you find any difficulties then please do comment your queries or problem via the comment section, till then stay tuned to techthings.org for more such valuable articles. 

Share if you have more information about the Symlinks and Hardlinks.

Leave a Reply

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