Showing posts with label Linux ln. Show all posts
Showing posts with label Linux ln. Show all posts

Friday, 2 August 2019

Linux - Soft links

Soft links in Linux/UNIX flavors is similar to a shortcut in windows. When we access the soft link file, it will redirect to the original file.
The main use of it is to access a particular file multiple times and by multiple users, which is not present in the present working directory.
Practical usage,
                Talk about any practical files already available in /bin or /sbin or /etc linked files.

Important notes on soft links: 
Soft link file size and original file size is different.
The inode number is different for soft link file and original file.
If we remove soft link file, we can access data through an original file.
 
Syntax :
ln –s actual-file soft-link-file

For example: 
[root@sys2 ~]# ls -l abclearn
-rw-r--r--. 1 root root 16 Jan 28 19:55 abclearn 
[root@sys2 ~]# ln -s abclearn abcsoft

[root@sys2 ~]# ls -l ab*
-rw-r--r--.    1 root root 16 Jan 28 19:55 abclearn
lrwxrwxrwx. 1 root root  8 Jan 28 19:56 abcsoft -> abclearn

In the above output,
The highlighted portion indicates the permissions to that linked file.
The starting letter ‘l’ indicates that it is a soft link file.

Hard linking a file

Let’s take a case,
We are working on updating particular software packages on Linux machine. And unexpectedly, a couple of important files got deleted by the administrator.
During this situations, hard linked files will come handy as a backup.

Note: 
Generally, the hard link file is used for the backup purpose of small and important configuration files like /etc/passwd, /etc/shadow etc.

Command Syntax:
ln actual-file hard link-file

Example:
[root@sys2 ~]# ln abclearn hardln [root@sys2 ~]# ls -l hardln
-rw-r--r--. 2 root root 16 Jan 28 19:55 hardln

[root@sys2 ~]# ls -l abclearn
-rw-r--r--. 2 root root 16 Jan 28 19:55 abclearn

In the above-highlighted output,
We can see the properties of both files are exactly same including time stamp also.
Best practice,
Creating hard links for the important files is always the best practice.

Important point to remember about hard links:
  • It is a permanent link to the original file.
  • Hard Link file size and original file size is same.
It means, any changes made to actual file content will be updated in hard link file also.We don’t have to again go and edit it.
  • Inode number is same for both original and hard link file.