Thursday, May 7, 2015

Soft links hard links


1. What are links in Unix?
    A link in UNIX is a pointer to a file. Like pointers in any programming languages, links in UNIX are pointers pointing to a file or a directory . Creating links is a kind of shortcuts to access a file. The two different types of links in UNIX are:
  • Soft Links or Symbolic Links
  • Hard links

2. How to create Soft links and hard links? And how do we access them?
     Say, you have a file named "file1" with the following contents:
$ cat file1
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
Note: Soft links can be created on non-existent files as well.

3. "file2" and "file3" being the linked files, can I say which is a soft link & which is hard link?
   Yes. When you do the listing of the files with "-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
   If you notice file3, it shows "->" towards file1. This indicates file3 is a soft-link of file1.  In case of file2, if you notice the inode numbers of file1 and file2, they are the same. Same inode number indicates file1 and file2 are hardlinks. Also note, the link count of these files is 2.

4. file1 and file2 are of the same size. In case of the file "file3", the file size is being shown as 5. How is it 5?
      The file size of a soft linked file is the length of the filename of the original file. In this case, the original file "file1" is of length 5. In a soft linked file, the location where the file content is to be stored, the file name of the original file gets stored, and hence the file size is so.

5. file1 and file2 are hardlinks? Can we say which is the original file and which is the hard-linked file?
    No. We cannot say which is the original file and which one was the hard-linked file. Once a hard-link 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.

6. Will deleting the file "file1" make "file2" and "file3" inaccessible?
   On deleting the file "file1", the soft linked file "file3" will become inaccessible. However, the hard linked file "file2" can still be accessed. - See more at: http://www.theunixschool.com/2012/03/soft-links-hard-links-all-about-inodes.html#sthash.JVsIPdJ1.dpuf
What are links in Unix?
    A link in UNIX is a pointer to a file. Like pointers in any programming languages, links in UNIX are pointers pointing to a file or a directory . Creating links is a kind of shortcuts to access a file. The two different types of links in UNIX are:
  • Soft Links or Symbolic Links
  • Hard links

2. How to create Soft links and hard links? And how do we access them?
     Say, you have a file named "file1" with the following contents:
$ cat file1
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
Note: Soft links can be created on non-existent files as well.

3. "file2" and "file3" being the linked files, can I say which is a soft link & which is hard link?
   Yes. When you do the listing of the files with "-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
   If you notice file3, it shows "->" towards file1. This indicates file3 is a soft-link of file1.  In case of file2, if you notice the inode numbers of file1 and file2, they are the same. Same inode number indicates file1 and file2 are hardlinks. Also note, the link count of these files is 2.

4. file1 and file2 are of the same size. In case of the file "file3", the file size is being shown as 5. How is it 5?
      The file size of a soft linked file is the length of the filename of the original file. In this case, the original file "file1" is of length 5. In a soft linked file, the location where the file content is to be stored, the file name of the original file gets stored, and hence the file size is so.

5. file1 and file2 are hardlinks? Can we say which is the original file and which is the hard-linked file?
    No. We cannot say which is the original file and which one was the hard-linked file. Once a hard-link 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.

6. Will deleting the file "file1" make "file2" and "file3" inaccessible?
   On deleting the file "file1", the soft linked file "file3" will become inaccessible. However, the hard linked file "file2" can still be accessed. - See more at: http://www.theunixschool.com/2012/03/soft-links-hard-links-all-about-inodes.html#sthash.JVsIPdJ1.dpuf

   If you notice file3, it shows "->" towards file1. This indicates file3 is a soft-link of file1.  In case of file2, if you notice the inode numbers of file1 and file2, they are the same. Same inode number indicates file1 and file2 are hardlinks. Also note, the link count of these files is 2.

4. file1 and file2 are of the same size. In case of the file "file3", the file size is being shown as 5. How is it 5?
      The file size of a soft linked file is the length of the filename of the original file. In this case, the original file "file1" is of length 5. In a soft linked file, the location where the file content is to be stored, the file name of the original file gets stored, and hence the file size is so.

5. file1 and file2 are hardlinks? Can we say which is the original file and which is the hard-linked file?
    No. We cannot say which is the original file and which one was the hard-linked file. Once a hard-link 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.

6. Will deleting the file "file1" make "file2" and "file3" inaccessible?
   On deleting the file "file1", the soft linked file "file3" will become inaccessible. However, the hard linked file "file2" can still be accessed. - See more at: http://www.theunixschool.com/2012/03/soft-links-hard-links-all-about-inodes.html#sthash.JVsIPdJ1.dp
http://www.theunixschool.com/2012/03/soft-links-hard-links-all-about-inodes.html

No comments:

Post a Comment