- Soft Links (or) Symbolic Links
- Hard Links
- "file2" and "file3" being the linked files, can I say which is soft link & which is hard link?
- File1 and file2 are hard links? Can we say which is the original file and which is the hard linked file?:
- How is it possible that while deleting the original file, still the hard linked file is accessible and the soft linked file is not?
- Soft links can span across File systems whereas hard links can be created only within a file system. Why so?
- Does soft links and hard links work only at the file level? Does it work at the directory level as well?
What are links in Linux?
A Link in Linux is
a pointer to a file. Like Pointers in any programming languages, links in
Linux are pointers pointing to a file or a directory. Creating Links is a Kind
of shortcuts to access a file. The two different type of links in UNIX are.
Difference
Between Soft links and hard links:
Soft Links
|
Hard Links
|
Soft Links Can be
created across file systems
|
Had Links can be
created only within the File System
|
Soft Link has a
different inode number than the original file
|
Had links have
the same inode number as the original File
|
On deleting the
original file, soft links cannot be accessed
|
On deleting the
original file, had linked file can still be accessed
|
Soft links need
extra memory to store the original file name as its data
|
Had links do not
need any extra data memory to save since
it uses links
|
Source file need
not exist for soft link file creation
|
Source file
should exist
|
Can be created on
a file or on a directory
|
Can be created
only on files, not on directories
|
Access to the
file is slower due to the overhead to access
file
|
Access to the
file is faster compared to soft link
|
How do we create
Soft Links and Hard Links? And How do we access them?
We have a file
named "file1" with the following contents:
$ cat file1
Welcome
Welcome
To create a hard link of file1:
$ ln file1 file2
To create a soft link of file1:
$ ln -s file1 file3
Once the links are
created, the linked files contain the same content as of the original file.
See below
$ cat file2
welcome
$ cat file3
Welcome
welcome
$ cat file3
Welcome
Note: Soft links
can be created on non-existent files as well.
Yes. When we do the
listing of the files with "ls -li" option:
$ ls -li
total 20
9962464 -rw-r--r-- 2 guru users 8 Mar 9 file1
9962464 -rw-r--r-- 2 guru users 8 Mar 9 file2
9962471 lrwxrwxrwx 1 guru users 5 Mar 9 file3 -> file1
total 20
9962464 -rw-r--r-- 2 guru users 8 Mar 9 file1
9962464 -rw-r--r-- 2 guru users 8 Mar 9 file2
9962471 lrwxrwxrwx 1 guru users 5 Mar 9 file3 -> file1
If you notice
"file3" shows "->" towards "file1". This
indicates file3 is a soft-link to file1. In case of file2, if we notice the
inode numbers of file1 and file2, they are the same. Same indoe number
indicates file1 and file2 are hard links. Also note, the link count of these
files is 2.
No. We can not say
which is the original file and which one is hard-linked file. Once a
hard-linked is created, it is like 2 files pointing to the same location. In
fact, once a hard link is created on a file, using the term "original
file" is actually incorrect.
Let us look into
the inode structure of the hard and soft links to understand in details.
Fig1: Hard link
representation ( Files pointing to indoe, in turn pointing to data location)
File1 and File2 are
hard links, As we know, they both have the same inode number ( 9962464) and
hence they both point to the same indoe structure. One of the parameters in
the inode tells the location of the file contents, and hence the contents are
accessed by both the files.
Now, say we try to
delete the file "File2". When the file is deleted, only the link
connecting of File2 to the indoe structure
gets disconnected. However, the inode and the file contents and the
link of FIle1 are still in tact, and
hence the file is still accessible.
However, the same
is not the case with the soft link. In case of Soft Link.
Fg2: Soft Link
Representation (Files pointing to inodes, in turn pointing to data location)
File1 and File3
both have different inode numbers. These different inode numbers point to
different data locations. File1 location points to the contents of the file.
However, if you see the content location of File3, it contains
"File1" which is nothing but the original file from which the soft
link is formed. Now, when "File3" is accessed, through its inode
metadata, it gets the name "File1" and searches for
"File1" and reaches to the content of "File1" Hence once
the "File1" is deleted, "File3" becomes dangling meaning
it points to something which does not exist. And so, the soft link becomes
inaccessible.
For these same
reasons, we can create a soft link on an non-existent file, however the same
is not possible for hard links.
Hard links refers
to its file using the inode number. Inode numbers are specific within a file
system. This means a file "f1" in file system "X" can have
the same inode numbers as of a file "f2"
in file system "Y". This is very much possible. For this reason, had
links cannot be shared across file systems.
However, the same
is not true for soft links, Soft links refer to the files using the file name
as seen in the earlier case. Hence a soft links can be accessed across the
file systems.
Hard links works
only at the file level. However, soft links works at the directory level as
well. This particular use of soft links, to create links at the directory
level, serves us a shortcut for many frequently used directories.


No comments:
Post a Comment