soft links and hard links in linux

Error message

  • Notice: Undefined index: taxonomy_term in similarterms_taxonomy_node_get_terms() (line 518 of /var/www/html/sites/all/modules/similarterms/similarterms.module).
  • Notice: Undefined index: 0 in similarterms_list() (line 221 of /var/www/html/sites/all/modules/similarterms/similarterms.module).
  • Notice: Undefined offset: 1 in similarterms_list() (line 222 of /var/www/html/sites/all/modules/similarterms/similarterms.module).
Satish Tiwary's picture
hard links and sofft links in linux

This tutorial is all about how to create  and manage Soft links and Hard links to files in your linux system.Names for files are stored on disk in directories.Only the names of Files are stored in directory;the actual disk space is somewhere else.So it clearly means that  the names and the actual storage for the things being named are in separate places.The name of a file is kept in a directory separate from the storage space i mean hard disk For the things it names due to which multiple names and names in multiple directories; all the names refer to the same storage space.The actual data stored in a file or directory in linux machine disk is managed by data structers "Inodes".So as we know a user can linked a file with many files, so it becomes difficult to find original file when a file is chain linked with so many files.So here we will use readlink utility to find all the linked files.

Note: Soft link is also knows as Symbolic link.

 

In this post, We will have a look at the following things.

  • How to create hard Links?
  • How to create Soft links?
  • Difference between soft and hard links
  • Identify links and know their type
  • Understand the difference between copying and linking files
  • How to use links for system admin tasks.
  • How to use of readlink utility.

 

Note:Before going through this post you must have idea about Inode.You can take our notes on Inode from

Tutorial on Inode.

 

Linux/Unix files consistes of two Parts:

1.Data Part

2.File Name Part

The Data Part is associated with Inode.

The Inode carries the map of where the data is.

The File name part carries a name and a associated inode number.

 

Hard Link Notes:

When more than one filename can reference the same inode number; these files are said to be 'hard linked' together.

 

To explain and Understand hard link in detail we create two file.

file1.txt  and  file2.txt  and then we will learn how to link them,manage them and see the statistics of both file after linking.

We use ln command line utility to create Hard Links.

 

[root@satish test]# cat > file1.txt
hii this is file1.txt ready for hard linking test.
tested by slashroot.in
 
[root@satish test]# ls -lh file1.txt
-rw-r--r-- 1 root root 74 Feb 18 03:12 file1.txt

 

[root@satish test]# ls -lih file1.txt
11010444 -rw-r--r-- 1 root root 74 Feb 18 03:12 file1.txt
 

So now you can see clearly the

  • content of file1.txt,
  • size of file1.txt which is 74 bytes
  • Permission of file1.txt which is 644
  • Inode number of the File1.txt is 11010444

 

Now how to Hard link file1.txt to file2.txt?

[root@satish test]# ln file1.txt file2.txt

so above command is used to hard link one file to another.

here i have hard linked file1.txt to file2.txt.

Now Let see the difference and similaritry between two hard linked file. i.e file1.txt and file2.txt.

[root@satish test]# ls -lih
total 8.0K
11010444 -rw-r--r-- 2 root root 74 Feb 18 03:12 file1.txt
11010444 -rw-r--r-- 2 root root 74 Feb 18 03:12 file2.txt
[root@satish test]# cat file2.txt
hii this is file1.txt ready for hard linking test.
tested by slashroot.in
[root@satish test]#
[root@satish test]# stat file1.txt
  File: `file1.txt'
  Size: 74              Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 11010444    Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-02-18 03:26:48.000000000 +0530
Modify: 2013-02-18 03:12:36.000000000 +0530
Change: 2013-02-18 03:20:04.000000000 +0530
 
[root@satish test]# stat file2.txt
  File: `file2.txt'
  Size: 74              Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 11010444    Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-02-18 03:26:48.000000000 +0530
Modify: 2013-02-18 03:12:36.000000000 +0530
Change: 2013-02-18 03:20:04.000000000 +0530

From above output we can clearly got the conclusion that for hard link:

  • Inode number does not changes i.e same Inode Number.
  • Same Permission for both files
  • Same data.
  • Same Size
  • Same Creation Time.
  • Only name is Different.

 

Note: Hard Link cannot link Directories and files on different file system.

 

Soft Link Notes:

Now to create soft link we need two files again for testing purpose. Take the file1.txt again and Soft link it with file3.txt this time.

 

How to create soft link?

We use ln command line utility with option  "-s" to create soft link.

Soft Link use File names or Directory.

[root@satish test]# ln -s file1.txt file3.txt

 

[root@satish test]# ls -lih
total 4.0K
11010444 -rw-r--r-- 1 root root 74 Feb 18 03:12 file1.txt
11010445 lrwxrwxrwx 1 root root  9 Feb 18 03:36 file3.txt -> file1.txt
[root@satish test]# cat file3.txt
hii this is file1.txt ready for hard linking test.
tested by slashroot.in
[root@satish test]#
[root@satish test]# stat file1.txt file3.txt
  File: `file1.txt'
  Size: 74              Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 11010444    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-02-18 03:38:42.000000000 +0530
Modify: 2013-02-18 03:12:36.000000000 +0530
Change: 2013-02-18 03:37:14.000000000 +0530
  File: `file3.txt' -> `file1.txt'
  Size: 9               Blocks: 0          IO Block: 4096   symbolic link
Device: 803h/2051d      Inode: 11010445    Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-02-18 03:49:16.000000000 +0530
Modify: 2013-02-18 03:36:54.000000000 +0530
Change: 2013-02-18 03:36:54.000000000 +0530
[root@satish test]#
[root@satish test]# file file1.txt
file1.txt: ASCII text
[root@satish test]# file file3.txt
file3.txt: symbolic link to `file1.txt'
[root@satish test]#

 

From above output we can clearly see that:

  • Both Files have different Inode Numbers.
  • Different permissions.
  • Different Size.
  • Same Content.
  • Different name

 

How to Create link to Directories?

First create two directory dir1 and dir2 and link them using ln command with option -s.

For testing Purpose create four files inside dir1 to see that whether it is linked with dir2 or not.

 

[root@satish test]# mkdir dir1
[root@satish test]# mkdir dir2
[root@satish test]# ln -s dir1 dir2

Above command is used to link directories.

[root@satish test]# tree
.
|-- dir1
|   |-- a
|   |-- b
|   |-- c
|   `-- d
`-- dir2
    `-- dir1 -> dir1

2 directories, 5 files

 

[root@satish test]# stat dir1 dir2
  File: `dir1'
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: 803h/2051d      Inode: 11010446    Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-02-18 04:23:34.000000000 +0530
Modify: 2013-02-18 04:16:49.000000000 +0530
Change: 2013-02-18 04:16:49.000000000 +0530
  File: `dir2'
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: 803h/2051d      Inode: 11010447    Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-02-18 04:23:34.000000000 +0530
Modify: 2013-02-18 04:16:03.000000000 +0530
Change: 2013-02-18 04:16:03.000000000 +0530
[root@satish test]# ls -li dir1
total 0
11010444 -rw-r--r-- 1 root root 0 Feb 18 04:16 a
11010445 -rw-r--r-- 1 root root 0 Feb 18 04:16 b
11010449 -rw-r--r-- 1 root root 0 Feb 18 04:16 c
11010450 -rw-r--r-- 1 root root 0 Feb 18 04:16 d
 
[root@satish test]# ls -li dir2
total 0
11010448 lrwxrwxrwx 1 root root 4 Feb 18 04:16 dir1 -> dir1

Note: Soft Linking a directory ti another directory can take place in many ways. But The right way to soft link directory is to link a directory to other when the other directory is not already created.

 

How to use readlink command in linux?

 

[root@satish ~]# readlink /etc/grub.conf
../boot/grub/grub.conf

 

[root@satish ~]# readlink -e /etc/grub.conf
/boot/grub/grub.conf

 

[root@satish ~]# readlink -f /etc/grub.conf
/boot/grub/grub.conf

 

It show us the linked file  to simplest form.

Note:That  "-e"  and "-f"  option activates the canonicalize mode

 

Rate this article: 
Average: 4.7 (6 votes)

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.