HARD LINKS AND SYMBOLIC LINKS
###############################
NOTE: symlinks/softlinks/symbolic links are all the same and similar to shortcuts
I will explain hard and symbolic links (also known as soft links) but first you need to look at “whats a file?”.
Best explanation for everyone: http://www.geekride.com/hard-link-vs-soft-link/
Best explanation: http://linuxgazette.net/105/pitcher.html
First whats a file?
====================
Think of this relationship. This is all a filename is. In the Filesystem a file points to an inode, which has some information about the data and the file and also the inode points to the data addresses. Then the data addresses actually hold the data.
FILENAME—->INODE—->DATA
What does each part contain?
* FILENAME has inode# and filename
|
| which points to
|
V
* INODE has inode#, permission bits, etc., data addresses
|
| which points to
|
V
* DATA has its address and data
Filename is what we are interacting with, which has in it its inode#
Whats a hardlink?
===================
Easiest way to explain: Multiple FILENAMES that point to the same INODE!
FILENAME1 —-> INODE —-> DATA
FILENAME2 —–/ |
FILENAME3 ——/
Notice how filename1,2 and 3 point to the same inode which points to the same data
When you “ls -l” (the linux list files command) any of those files
ls -l
1394 rwxrwxrwx 3 FILENAME3
1394 rwxrwxrwx 3 FILENAME2
1394 rwxrwxrwx 3 FILENAME1
Notice the 3 after the permission bits, that means that the inode has 3 filenames pointed to it.
All of the files have the same inode number, because they all point to the same inode.
The original file is FILENAME1, but now they are all equaly the same, you can say they are all equaly original – but I wouldnt say that, its just to give you the idea that they are all the same – HARDLINK multiply the . If I delete FILENAME3 and FILENAME2 leaving me with the original FILENAME1 (the link counter will go to 1 and the inode # will still be 1394) then I still have the same data – LIKEWISE – if I delete FILENAME1 and FILENAME2, I still have all of the data with FILENAME3.
With hardlinks no single file is more important, they are all the original copy in my opinion.
Another important note is a hardlink cant be made on a directory.
A note about deleting:
———————–
Deleting a hardlink or the original file doesnt hurt the data as long as another hardlink (or the original file still exists). So deleting a hardlink your still left with the original file, and deleting the original file well your still left with the hardlink. Remember the hardlinks and the original files can all be thought of as the original file – look at the diagram above to make sense of it.
When deleting a file that has hardlinks make sure to delete the files until the link counter is down at 0, when its 0 the file doesnt exist anymore. OF you wont see the link counter at 0, but you will see it at 1, and when you see it at 1 and you issue an RM command on it, it will decrement to 0 and disappear.
So whats a symlink (also known as softlink)?
============================================
Easy to understand because they are link windows shortcuts ( a new file that points to another file)
Those are harder to draw up.
The idea is the Operating system knows that when it seems a certain piece of DATA, that this file is a symlink. Just like when an OS sees a jpg it knows its a picture (kind of the same idea just more low level and closer to the kernel for the symlink).
The DATA part of this special file points to another FILENAME
A simple look:
(Just like a regular file )==that points to====>(               ORIGINAL FILENAME                     )
A deeper look:
(This part is the sym link)====redirects to====>(The full original filename with the inode & data part)
An even deeper look:
(FILENAME—>INODE—>DATA)====redirects to====>(ORIGINAL FILENAME—>ORIGINAL INODE—->ORIGINAL DATA)
For an even deeper look go to link above.
So imageine a file called “ORIGINAL FILENAME” and we made a symlink called “FILENAME” which points to “ORIGINAL FILENAME”. When you open FILENAME, the OS looks at the FILENAMES INODE which points to the FILENAME’s DATA which then says “hey I am symlink point at ‘ORIGINAL FILENAME’ ” So then the OS says okay likes open up “ORIGINAL FILENAME”, and then it does its regular openfile procedure on the actual regular file – just like opening a regular file without a symlink – so basically it then opens the “ORIGINAL FILENAME” which points it at “ORIGINAL FILENAME” INODE and that points to all of the “ORIGINAL FILENAME” DATA ADDRESSES and then it opens the ORIGINAL DATA Addresses
Take an example where ORG_FILENAME is the original file and FILENAME1 and 2 and 3 are the symlinks.
(FILENAME1—>INODE #1234—>DATA) == redirects to =\
(FILENAME2—>INODE #1235—>DATA) == redirects to ===>(ORG_FILENAME—>ORIGINAL INODE #1000—->ORIGINAL DATA)
(FILENAME3—>INODE #1236—>DATA) == redirects to =/
When you “ls -l” (the linux list files command) any of those files
1394 rwxrwxrwx 1 ORG_FILENAME
1395 lwxrwxrwx 1 FILENAME1
1396 lwxrwxrwx 1 FILENAME2
1397 lwxrwxrwx 1 FILENAME3
SYMLINKS have new inode numbers
Note there is no Link counter here, they are all at 1
A note about deleting:
———————-
Deleting the symlink you still have the original file, but deleting the original file (ORG_FILENAME from above example) makes all of the other links “broken” aka they dont work anymore, might as well delete them too.
In other words:
################
Standard files are a pointer from the filesystem to an inode which in turn point to physical data. The file component stores its link to the filesystem (essentially its path) and a link to the inode.
HARD LINKS
===========
Hard-links, are just like files. They’re just an additional pointer directly to an inode.
For files only & you cannot create on different partition ( it should be on same partition ) & got same inode number as original
If the real copy is deleted the link will work ( because it act as original file )
HOW TO MAKE:
Note when making the destination file or directory doesnt need to be there, infact its best practice for it not to be there, unless your pointing the link to a new source.
# ln <file with data, the original> <the new file, the hardlink>
# ln /etc/fstab /home/admin/the-new-mega-fstab
Of course after they are made they can both be thought of as the original
SOFTLINK aka SYMBOLIC LINKS aka SYM LINKS
===========================================
Symbolic-links is a new file with its own inode and data, the data part is recognized by the kernel and it points to the original filename. The filename of the original then points to the inode and the data from there.
Symbolic links are also called softlinks
You can make links for files & folder & you can create link (shortcut) on different partition & got different inode number from original.
If real copy is deleted the link will not work.
Note when making the destination file or directory doesnt need to be there, infact its best practice for it not to be there, unless your pointing the link to a new source.
HOW TO MAKE FILE:
Note when making the destination file or directory doesnt need to be there, infact its best practice for it not to be there, unless your pointing the link to a new source.
# ln -s <file with data, the original> <the new file, the softlink>
# ln -s /etc/fstab /home/admin/the-new-mega-fstab
Of course after they are made they can both be thought of as the original
HOW TO MAKE FOR DIRECTORY:
# ln -s <directory with data, the original> <the new directory, the softlink>
# ln -s /etc/ /home/admin/the-new-etc
so then doing “cd /home/admin/the-new-etc” will put you into /etc”
##############################################################
SOME TESTING
=============
TO MAKE SYMLINK (SYMBOLIC LINK) – new inode
ln -s <original file with data>  <new file>
TO MAKE HARD LINK: – same inode new data pointer
ln <original file with data> <new file>
# TEST FROM /tmp
cd /tmp
# MAKE FILE AND SYMBOLIC LINK AND HARDLINK – NO PROBLEM
echo “test file original” > testfile
ln testfile testfileh
ln -s testfile testfiles
# MAKE FOLDER AND SYMBOLIC AND HARDLINK (HARDLINK HAS A PROBLEM)
mkdir testdir
ln testdir/ testdirh  # FAILED CANT MAKE HARD LINK TO A DIRECTORY
ln -s testdir/ testdirs
# PUT SOME FILES INTO DIRECTORY VIA SYMBOLIC LINK (VIA NORMAL DIR WOULD OF WORKED TOO)
echo “testfile” > testdirs/filea
echo “testfile” > testdirs/fileb
ln -s testdir testdirs2 # WORKS
ln -s testdirs testdirs3 # WORKS
# BELOW WORKS, BUT HOW??? BECAUSE IT ACTUALLY JUST MAKES A SYMBOLIC LINK!
# HARDLINK TO SYMLINK IS JUST A HARDLINK
ln testdirs3 testdirh3
# GETS ME INTO THE testdir FOLDER JUST LIKE IT WAS SYMLINK
cd testdirh3
cd ..
##### OUTPUT OF BOTH ######
# notice hardlinks have same inode numbers
# — hardlink from testfile(original) to testfileh(the hardlink), same inode 13290
# — hardlink from testdirs3(symlink from testdir) to testdirh3(the hardlink), same inode 13296
# symlinks have a ->
# hardlinks increment the link counter (# after the rwxrwxrwx) for the link and original file
NAS:/tmp# ls -lisah test*
13296    0 lrwxrwxrwx 2 root root    8 2013-08-04 21:14 testdirh3 -> testdirs
13292    0 lrwxrwxrwx 1 root root    8 2013-08-04 21:13 testdirs -> testdir/
13293    0 lrwxrwxrwx 1 root root    7 2013-08-04 21:14 testdirs2 -> testdir
13296    0 lrwxrwxrwx 2 root root    8 2013-08-04 21:14 testdirs3 -> testdirs
13290 4.0K -rw-r–r– 2 root root   19 2013-08-04 21:12 testfile
13290 4.0K -rw-r–r– 2 root root   19 2013-08-04 21:12 testfileh
13291    0 lrwxrwxrwx 1 root root    8 2013-08-04 21:12 testfiles -> testfile
NAS:/tmp# file test*
testdir:   directory
testdirh3: symbolic link to `testdirs’
testdirs:  symbolic link to `testdir/’
testdirs2: symbolic link to `testdir’
testdirs3: symbolic link to `testdirs’
testfile:  ASCII text
testfileh: ASCII text
testfiles: symbolic link to `testfile’
testdir:
total 16K
15094 4.0K drwxr-xr-x 2 root root 4.0K 2013-08-04 21:13 .
12289 4.0K drwxrwxrwt 5 root root 4.0K 2013-08-04 21:14 ..
15095 4.0K -rw-r–r– 1 root root    9 2013-08-04 21:13 filea
15096 4.0K -rw-r–r– 1 root root    9 2013-08-04 21:13 fileb

Leave a Reply

Your email address will not be published. Required fields are marked *