Friday, July 4, 2014

Unix Shell: Hard Soft Link

1.Hard Link:
In unix system, file will be separated into 2 parts: inode storing the file content, and file name, storing the file content address.

For hardlink, it just stores the file content address. If there are multiple filenames linking to same file content, any change to one of them will affect the other.

1) Create a file: oa, containing the string "Hello world!"
2) Create a hard link: hl_oa, which is linking to file oa. Right now, both "oa" and "hl_oa" have the address of same file data.
3) List all files in local directory, it indicates that both "oa" and "hl_oa" have the same size.
4) Add execution permission to hl_oa
5) List all files, we found that both "oa" and "hl_oa" permission get updated
6) Append another line of string to hl_oa
7) List all files, we found that both "oa" and "hl_oa" size get updated, meaning that if affect both files.
8) 9) Print out file content of both files, both files have the new appended string
10)Remove the hard_link file
11) List all files, we found that oa is still there.

Note: In unix system, given a part of file content, it maintains a link count about how many file names whose addresses are linking to this piece of content. If there are more than 1 link, then the file content will not be removed until the link count reduce to 0.

In memory:
oa --------------------------> file content <---------------------hl_oa
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo "Hello world!" > oa  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ln oa hl_oa  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt  
 total 8  
 -rw-rw-r-- 2 aubinxia aubinxia 13 Jul 4 15:47 oa  
 -rw-rw-r-- 2 aubinxia aubinxia 13 Jul 4 15:47 hl_oa  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ chmod +x hl_oa  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt  
 total 8  
 -rwxrwxr-x 2 aubinxia aubinxia 13 Jul 4 15:47 oa  
 -rwxrwxr-x 2 aubinxia aubinxia 13 Jul 4 15:47 hl_oa  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo "Hello world!" >> hl_oa  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt  
 total 8  
 -rwxrwxr-x 2 aubinxia aubinxia 26 Jul 4 15:48 oa  
 -rwxrwxr-x 2 aubinxia aubinxia 26 Jul 4 15:48 hl_oa  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat oa  
 Hello world!  
 Hello world!  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat hl_oa  
 Hello world!  
 Hello world!  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ rm hl_oa
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt
 total 4
 -rwxrwxr-x 1 aubinxia aubinxia 26 Jul  4 15:48 oa

2.soft link (symbolic link):
Soft link is quite different from the hard link. Not like hard link who stores the address of file content, soft link just stores address of another file name, who in turn stores the address of the file content in "inode".

1) Create one file called "oa" who has only one line of string
2) "ln -s" is used to create soft link. In this case, we create one soft link called "sl_oa" referring to "oa"
3) List all files. "oa" is referring to the real file content about one line of string, sl_oa is referring to address of file name "oa" who in turn stores the address of file content. So sl_oa's size is just 2.
4) Remove the write permission from oa
5) List all files. It indicates that sl_oa is not affected at all, since it is not pointing to the real file content.
6) Add the write permission back to oa
7) Append another line of string to file oa
8) List all files. It indicates that file oa's size get doubled, since it is pointing to the real file content, and we just appended another line of string there. But sl_oa's size is not changed at all, because it is not referring to the real file content.
9) Append another line of string to file sl_oa, it will append the line of string to the real file content.
10) List all files. Soft link sl_oa's size still doesn't get changed, since it is not referring to the real file content. oa's size is updated to 39, since the content it refer to has a new line of string.
11 -12 ) Print out files oa and sl_oa. Both files have same content, when printing out oa, it will just print out the file content. When printing out sl_oa, it will follow the address to file name "oa"'s space, which store address of real file content. Then it will follow that address to reach real file content space, and print out.

In memory:
sl_oa    -------------------------------> oa --------------------------> file content

13) Remove file oa, right now sl_oa has a "broken link", since both oa and file content get removed now.
14) List all files, sl_oa is still pointing to oa
15) Print out sl_oa's content, since it has a broken link, it can not reach to the real file content. System complains.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo "Hello world!" > oa  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ln -s oa sl_oa  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt  
 total 4  
 -rw-rw-r-- 1 aubinxia aubinxia 13 Jul 4 16:03 oa  
 lrwxrwxrwx 1 aubinxia aubinxia 2 Jul 4 16:04 sl_oa -> oa  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ chmod -w oa  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt  
 total 4  
 -r--r--r-- 1 aubinxia aubinxia 13 Jul 4 16:03 oa  
 lrwxrwxrwx 1 aubinxia aubinxia 2 Jul 4 16:04 sl_oa -> oa  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ chmod +w oa  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo "Hello world!" >> oa  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt  
 total 4  
 lrwxrwxrwx 1 aubinxia aubinxia 2 Jul 4 16:04 sl_oa -> oa  
 -rw-rw-r-- 1 aubinxia aubinxia 26 Jul 4 16:05 oa  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo "Hello world!" >>sl_oa  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt  
 total 4  
 lrwxrwxrwx 1 aubinxia aubinxia 2 Jul 4 16:04 sl_oa -> oa  
 -rw-rw-r-- 1 aubinxia aubinxia 39 Jul 4 16:05 oa  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat sl_oa  
 Hello world!  
 Hello world!  
 Hello world!  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat oa  
 Hello world!  
 Hello world!  
 Hello world!  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ rm oa  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt  
 total 0  
 lrwxrwxrwx 1 aubinxia aubinxia 2 Jul 4 16:04 sl_oa -> oa  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat sl_oa  
 cat: sl_oa: No such file or directory  

No comments:

Post a Comment