If a hard link is created to a file and then the original file is deleted
To understand the difference between hard and symbolic links, we must first understand what they are, why they exist, and how to create them. Show
As you see in the above output, $ cat source.file Welcome to OSTechNix1 displays the same data as $ echo "Welcome to OSTechNix" >source.file7. Let us check the inodes and permissions of $ cat source.file Welcome to OSTechNix1 and $ echo "Welcome to OSTechNix" >source.file7. $ ls -lia Sample output: total 12 11665675 drwxrwxr-x 2 sk sk 4096 Oct 17 11:39 . 4325378 drwxr-xr-x 37 sk sk 4096 Oct 17 11:39 .. 11665731 lrwxrwxrwx 1 sk sk 11 Oct 17 11:39 softlink.file -> source.file 11665692 -rw-rw-r-- 1 sk sk 21 Oct 17 11:39 source.fileCheck the inodes and permissions of symbolic link As we see in the above screenshot, the inode number ( $ cat source.file Welcome to OSTechNix6 vs $ cat source.file Welcome to OSTechNix7) and file permissions ( $ cat source.file Welcome to OSTechNix8 vs $ cat source.file Welcome to OSTechNix9) are different, even though the $ cat source.file Welcome to OSTechNix1 has same contents as $ echo "Welcome to OSTechNix" >source.file7. Hence, it is proved that soft link don't share the same inode number and permissions of original file. Now, remove the original file (i.e $ echo "Welcome to OSTechNix" >source.file7) and see what happens. $ rm source.file Check contents of the $ cat source.file Welcome to OSTechNix1 using command: $ cd test0 Sample output: $ cd test1Check symbolic link contents As you see above, there is no such file or directory called $ cat source.file Welcome to OSTechNix1 after we removed the original file (i.e $ echo "Welcome to OSTechNix" >source.file7). So, now we understand that soft link is just a link that points to the original file. The softlink is like a shortcut to a file. If you remove the file, the shortcut is useless. As you already know, if you remove the soft link, the original file will still be available. Suggested read:
How to create Hard LinkCreate a file called $ echo "Welcome to OSTechNix" >source.file7 with some contents as shown below. $ echo "Welcome to OSTechNix" >source.file Let us verify the contents of the file. $ cat source.file Welcome to OSTechNix The $ echo "Welcome to OSTechNix" >source.file7 has been created now. Now, let us create the hard link to the $ echo "Welcome to OSTechNix" >source.file7 as shown below. $ cd test4Create hard link Check the contents of $ ln -s source.file softlink.file9: $ cd test5 You see the $ ln -s source.file softlink.file9 displays the same data as source.file. Let us check the inode and permissions of $ ln -s source.file softlink.file9 and $ echo "Welcome to OSTechNix" >source.file7. $ ls -lia Sample output: $ cd test7Check the inodes and permissions of hard link Now, we see that both $ ln -s source.file softlink.file9 and $ echo "Welcome to OSTechNix" >source.file7 have the same the inodes number ( $ cat source.file Welcome to OSTechNix7) and file permissions ( $ cat source.file Welcome to OSTechNix9). Hence, it is proved that hard link file shares the same inodes number and permissions of original file. Heads Up: If we change the permissions on $ echo "Welcome to OSTechNix" >source.file7, the same permission will be applied to the $ ln -s source.file softlink.file9 as well. Now, remove the original file (i.e $ echo "Welcome to OSTechNix" >source.file7) and see what happens. $ rm source.file Check contents of $ ln -s source.file softlink.file9 using command: $ cd test9 Sample output: Check hard link contentsAs you see above, even if I deleted the source file, I can view contents of the $ ln -s source.file softlink.file9. Hence, it is proved that Hard link shares the same inode number, the permissions and data of the original file. So, what is the difference between Hard link and the normal copied file?You might be wondering why would we create a hard link while we can easily copy/paste the original file? Creating a hard link to a file is different than copying it. If you copy a file, it will just duplicate the content. So if you modify the content of a one file (either original or hard link), it has no effect on the other one. However if you create a hard link to a file and change the content of either of the files, the change will be seen on both. Let us have a look at the source.file. $ echo "Welcome to OSTechNix" >source.file0 The source file has a single line that says - Welcome to OSTechNix. Append a new line, for example "Welcome to Linux" in $ echo "Welcome to OSTechNix" >source.file7 or $ ln -s source.file softlink.file9. $ echo "Welcome to OSTechNix" >source.file1 Now check contents of both files. $ echo "Welcome to OSTechNix" >source.file2 $ echo "Welcome to OSTechNix" >source.file3Update contents of hard link See? The changes we just made on $ echo "Welcome to OSTechNix" >source.file7 are updated in both files. Meaning - both files (source and hard link) synchronizes. Whatever changes you do in any file will be reflected on other one. If you normally copy/paste the file, you will not see any new changes in other file. For more details, check the man pages. $ echo "Welcome to OSTechNix" >source.file4 ConclusionIn this guide, we have discussed what is soft link and hard link in Linux, how to create softlink and hardlink with example commands and finally we explained the difference between hardlink and normal copied file. Hope you got a basic idea of how to use symbolic or soft link and hard link in Linux. BASHCLICommand linehard linkLinuxLinux basicsLinux commandsLinux howtoLinux tipsln commandsoft linksymbolic linkUnixUnix basics What happens if you delete a hardlinked file?If the hard link gets deleted, nothing happens to the original file, as it still has its data in the hard drive. Vice versa, if the original file gets deleted, the hard link still refers to the location of the data in the hard drive, therefore the information is still saved.
What happen if we delete the original file is it still accessible from its hard link justify your answer?If you delete the original file, the soft link has no value, because it points to a non-existent file. But in the case of hard link, it is entirely opposite. Even if you delete the original file, the hard link will still has the data of the original file. Because hard link acts as a mirror copy of the original file.
What happens when you create a hard link to a text file and then you delete the text file answers the hard link will also be deleted the?A hard link will never point to a deleted file. A hard link is like a pointer to the actual file data. And the pointer is called "inode" in file system terminology. So, in other words, creating a hard link is creating another inode or a pointer to a file.
What happens when you create a hard link to a text file and then you delete the text file Mcq?Deleting the hard link does not delete the file it is hardlinked to and the file that was linked to remains where it is. The key is that creating the text file also adds a hard link.
|