Linux file links

Hi , welcome to linux file links tutorial.In this tutorial we are going to learn about
hard links,symbolic links and inodes. Understanding Inodes in linux is very important for understanding linux file links.
First we will discuss about inode contents, inode table and inode number, then we will discuss directories, creating and removing hard links and symbolic links.
lets start with Inodes

What is Inode?

In Linux meta information about a file is stored in data structure which is called an inode.
Metadata(or meta information) can include a name of file,owner of file, permissions on file, creation date etc. but name of file is stores in directory not in inode
So whenever a new file Linux not only stores its actual contents but it also stores it metadata information in inode.
If you want to see example of some meta data just type ls -l

[root@shahid security]# ls -l file
total 0
-rwxr-xr-x. 1 rob     rob      0 Feb 20 18:54 file

In above example you can see name of file, owner,permissions and creation date is metadata of file and all this data (except filename) is stored in inode.

inode table

When you create linux file system it will create a special table where all inodes will be stored.
This table is called inode table. To view more information about inode table like how many inodes are used and how many are free you can use command
df -i. See below example

[root@shahid security]# df -i
Filesystem              Inodes  IUsed    IFree IUse% Mounted on
/dev/mapper/rhel-root 18317312 108426 18208886    1% /
devtmpfs                253738    332   253406    1% /dev
tmpfs                   256209      5   256204    1% /dev/shm
tmpfs                   256209    436   255773    1% /run
tmpfs                   256209     13   256196    1% /sys/fs/cgroup
/dev/sda1               512000    327   511673    1% /boot
[root@shahid security]#

inode number

Every inode has a unique number which is called inode number. To view information about inode number use command ls -li
see below example

[root@shahid security]# ls -li
total 0
 2034611 -rwxr-xr-x. 1 rob     rob      0 Feb 20 18:54 file
 2034608 -rwxrwx-wx. 1 rob     shahid   0 Feb 20 18:53 file1
 2034610 -rw-rw-r--. 1 tim     tim      0 Feb 20 18:53 file2
37147162 -rw-r--r--. 1 michael michael  0 Feb 20 18:57 file3

In above example all four files have unique inode number . All this information except file name is stored in inode.

inode and file content

we have added some content to file1

[root@shahid security]# echo this is test content > file 1
[root@shahid security]# cat file
this is test content 1
[root@shahid security]#

In above example content in file is stored in different location somewhere on disk. This content is not stored in inode. Inode is only referencing that location in other words
inode is pointer and pointing to that location where this content is stored

Linux Directory as a table

As we discussed the name of file is stored in directory. Actually in linux directory is special file which contains a table which maps name of files to inodes
use command ls -ali to view contents in directory

[root@shahid security]# ls -ali
total 12
37147161 drwxrwxr-x.  3 shahid  shahid  4096 Feb 20 20:43 .
     139 drwx------. 17 shahid  shahid  4096 Feb 27 11:30 ..
 2034611 -rwxr-xr-x.  1 rob     rob       23 Mar  6 12:29 file
 2034608 -rwxrwx-wx.  1 rob     shahid     0 Mar  6 12:28 file1
 2034610 -rw-rw-r--.  1 tim     tim        0 Feb 20 18:53 file2
37147162 -rw-r--r--.  1 michael michael    0 Feb 20 18:57 file3

In above example you see one dot (.) which is mapping to itself two dots dotdot(..) is mapping to parent directory and rest of four files are
mapping to different inodes

hard links

To create hard link to file you can use command ln actual-name-of-file name-of-link
This command creates a hard link to a file which is an extra entry added to the directory. Hardlink is a new
file name mapped to an existing inode.

[root@shahid security]# ln file hardlink
[root@shahid security]# ls -ali
total 16
37147161 drwxrwxr-x. 3 shahid shahid 4096 Mar 6 12:39 .
139 drwx——. 17 shahid shahid 4096 Feb 27 11:30 ..
2034611 -rwxr-xr-x. 2 rob rob 23 Mar 6 12:29 file
2034608 -rwxrwx-wx. 1 rob shahid 0 Mar 6 12:28 file1
2034610 -rw-rw-r–. 1 tim tim 0 Feb 20 18:53 file2
37147162 -rw-r–r–. 1 michael michael 0 Feb 20 18:57 file3
2034611 -rwxr-xr-x. 2 rob rob 23 Mar 6 12:29 hardlink

In above example you can see new entry hardlink is added its inode number is 2034611 which is exactly same number for for file “file” meaning
both files have the same inode,both have same permissions,owner and same contents.
If you want to remove original file hardlink will still be there. The inode has a counter, which counts number of hardlink pointing to itself. When counter drops to zero and inode is empty

how to find hard link ?

if you want to find files by using their inode number you can use command find command .
For example from previous example we know inode number 2034611 . Below example
shows how to search for all files which points to inode 2034611. Please also remember that an inode
number is unique to its partition.

[root@shahid security]# find / -inum 2034611
/home/shahid/security/file
/home/shahid/security/hardlink
[root@shahid security]#

symbolic links

Symbolic links are sometimes referred as soft links. They do not link to inodes, but create a mapping name to
name. To create a Symbolic link we use command ln -s actual-file symlink-to-the-file.
See below example, the symbolic link has its own inode 37105952 whereas file1 has inode 2034608

[root@shahid security]# ls -ali
total 16
37147161 drwxrwxr-x.  3 shahid  shahid  4096 Mar  6 12:53 .
     139 drwx------. 17 shahid  shahid  4096 Feb 27 11:30 ..
 2034611 -rwxr-xr-x.  2 rob     rob       23 Mar  6 12:29 file
 2034608 -rwxrwx-wx.  1 rob     shahid     0 Mar  6 12:28 file1
 2034610 -rw-rw-r--.  1 tim     tim        0 Feb 20 18:53 file2
37147162 -rw-r--r--.  1 michael michael    0 Feb 20 18:57 file3
 2034611 -rwxr-xr-x.  2 rob     rob       23 Mar  6 12:29 hardlink
37105952 lrwxrwxrwx.  1 root    root       5 Mar  6 12:53 symlink-file1 -> file1

There are no meanings of permissions on symbolic link. Permissions of target file applies in this case
As we mentioned before that hardlinks are unique to its partition in other words hard links are limited to their own partition (because they point to an inode), whereas symbolic links
can link anywhere eg it can point to other file systems or even other network system

removing links

To remove linux file link you can use command rm name-of-link
see below example

[root@shahid security]# ls -ali
total 16
37147161 drwxrwxr-x.  3 shahid  shahid  4096 Mar  6 12:53 .
     139 drwx------. 17 shahid  shahid  4096 Feb 27 11:30 ..
 2034611 -rwxr-xr-x.  2 rob     rob       23 Mar  6 12:29 file
 2034608 -rwxrwx-wx.  1 rob     shahid     0 Mar  6 12:28 file1
 2034610 -rw-rw-r--.  1 tim     tim        0 Feb 20 18:53 file2
37147162 -rw-r--r--.  1 michael michael    0 Feb 20 18:57 file3
 2034611 -rwxr-xr-x.  2 rob     rob       23 Mar  6 12:29 hardlink
37147166 lrwxrwxrwx.  1 root    root      15 Feb 20 20:43 my_test_link -> sampledirectory
70330764 drwxr-xr-x.  2 root    root       6 Feb 20 20:37 sampledirectory
37105952 lrwxrwxrwx.  1 root    root       5 Mar  6 12:53 symlink-file1 -> file1
37147164 -rw-r--r--.  1 root    root       0 Feb 20 19:00 testfile
[root@shahid security]# rm my_test_link
rm: remove symbolic link âmy_test_linkâ? y
[root@shahid security]# rm hardlink
rm: remove regular file âhardlinkâ? y
[root@shahid security]# rm symlink-file1
rm: remove symbolic link âsymlink-file1â? y
[root@shahid security]# ls -ali
total 8
37147161 drwxrwxr-x.  3 shahid  shahid    90 Mar  6 13:00 .
     139 drwx------. 17 shahid  shahid  4096 Feb 27 11:30 ..
 2034611 -rwxr-xr-x.  1 rob     rob       23 Mar  6 12:29 file
 2034608 -rwxrwx-wx.  1 rob     shahid     0 Mar  6 12:28 file1
 2034610 -rw-rw-r--.  1 tim     tim        0 Feb 20 18:53 file2
37147162 -rw-r--r--.  1 michael michael    0 Feb 20 18:57 file3
70330764 drwxr-xr-x.  2 root    root       6 Feb 20 20:37 sampledirectory
37147164 -rw-r--r--.  1 root    root       0 Feb 20 19:00 testfile

This brings end to our tutorial. In this tutorial we have learnt about linux file links, (hard links symbolic link/soft link, inode, inode number , inode table , adding hard link and symbolic links and removing links
I hope you have enjoyed tutorial.Feel free to let us know if you find any any mistake in tutorial or it needs improvement.
Thanks for visiting our blog and see you in next tutorial

SUBSCRIBE OUR NEWSLETTER
I agree to have my personal information transfered to MailChimp ( more information )
Join us by subscribing to our newsletter and learn IT subjects for free
We hate spam. Your email address will not be sold or shared with anyone else.