Links: Hard Links: A Hard Link Is A Pointer To The File's I-Node. For Example, Suppose That We Have A File
Links: Hard Links: A Hard Link Is A Pointer To The File's I-Node. For Example, Suppose That We Have A File
As was mentioned in the section on file system structure, every file has a data structure (record)
known as an i-node that stores information about the file, and the filename is simply used as a
reference to that data structure. A link is simply a way to refer to the contents of a file. There are
two types of links:
Hard links: a hard link is a pointer to the file's i-node. For example, suppose that we have a file
a-file.txt that contains the string "This is file a_file.txt":
% cat a_file.txt
This is file a_file.txt
%
Now we use the link (ln) command to create a link to a_file.txt called b_file.txt:
% ls
./ ../ a_file.txt
% ln a_file.txt b_file.txt
% ls
./ ../ a_file.txt b_file.txt
The two names a_file.txt and b_file.txt now refer to the same data:
% cat b_file.txt
This is file a_file.txt
%
If we modify the contents of file b-file.txt, then we also modify the contents of file a-file.txt and vice
versa.
% vi b_file.txt
/*some modification is done in b_file.txt*/...
% cat b_file.txt
The file a_file.txt has been modified.
% cat a_file.txt
The file a_file.txt has been modified.
%
% vi a_file.txt
/*some modification is done in a_file.txt*/...
% cat a_file.txt
The file a_file.txt has been modified again!
% cat b_file.txt
The file a_file.txt has been modified again!
%
i-node file content
If we delete the a_file.txt then also b_file.txt can change and access the content of a_file.txt. Here, only
the pointer a_file.txt is deleted. but the i-node is pointed by b_file.txt.
Soft links (symbolic links): a soft link, also called symbolic link, is a file that contains the name of
another file. We can then access the contents of the other file through that name. That is, a symbolic link
is like a pointer to the pointer to the file's contents. For instance, supposed that in the previous example,
we had used the -s option of the ln to create a soft link:
% ls
./ ../ a_file.txt b_file.txt
Let us first add another symbolic link or soft link using the ln -s option:
% ln -s a_file.txt Symbolicb_file.txt
% ls -F
./ ../ a_file.txt b_file.txt Symbolicb_file.txt@
A symbolic link, that ls -F displays with a @ symbol, has been added to the directory. Let us
examine the contents of the file:
% cat Symbolicb_file.txt
The file a_file.txt has been modified again!
If we change the file Symbolicb_file.txt, then the file a_file.txt is also modified.
% vi Symbolicb_file.txt
...
% cat Symbolicb_file.txt
The file a_file.txt has been modified a third time!
% cat a_file.txt
The file a_file.txt has been modified a third time!
% cat b_file.txt
The file a_file.txt has been modified a third time!
%
If we remove the file a_file.txt, we can no longer access the data through the symbolic link
Symbolicb_file.txt:
% ls -F
./ ../ a_file.txt b_file.txt Symbolicb_file.txt@
% rm a_file.txt
rm: remove `a_file.txt'? y
% ls -F
./ ../ b_file.txt Symbolicb_file.txt@
% cat Symbolicb_file.txt
cat: Symbolicb_file.txt: No such file or directory
The link Symbolicb-file.txt contains the name a-file.txt, and there no longer is a file with that
name. On the other hand, b-file.txt has its own pointer to the contents of the file we called a-
file.txt, and hence we can still use it to access the data.
% cat b-file.txt
The file a-file.txt has been modified a third time!
The most significant drawback is that hard links cannot be created to link a file from one file system to
another file on another file system. A Unix file structure hierarchy can consist of several different file
systems (possibly on several physical disks). Each file system maintains its own information regarding
the internal structure of the system and the individual files on the system. Hard links only know this
system-specific information, which make hard links unable to span file systems. Soft links, on the other
hand, know the name of the file, which is more general, and are able to span file systems.
When deleting files, the data part isn't disposed of until all the filename parts have been deleted. There's a
count in the inode that indicates how many filenames point to this file, and that count is decremented by 1
each time one of those filenames is deleted. When the count makes it to zero, the inode and its associated
data are deleted.
Hard Links :
2.ls -l command shows all the links with the link column(Second) shows No. of links.
4.Removing any link just reduces the link count but doesn't affect other links.
2. ls -l command shows all links with second column value 1 and the link points to original file.
3. Link has the path for original file and not the contents.
4.Removing soft link doesn't affect anything but removing original file the link becomes dangling link
which points to nonexistant file.