What is the difference between softlink and hardlink in unix
It is like a shortcut in Windows which contains the path of the original file and not the contents. Hard link is a mirror copy of the original file. Deleting the original file will not impact anything, because the hard link file, will act as a mirror copy of the original file. Change the data on the hard drive and File 1 changes too. Now imagine you create a hard link to File 1. Because it's a hard link, it's a new file — we'll call it File 2.
But when you open File 2, the data is the same as File 1. And because it's a hard link, it points to the exact same spot on the hard drive. Effectively, a hard link gives you two, separate files that behave like different files. But it's better that you don't think of a hard link as a copy. Because there's an exception to the idea that they behave like separate files: if you make a change to File 1, File 2 will automatically be edited.
That happens because they're two separate files, but they're pointing to the exact same spot on the hard drive. If you go down to the Inode level and change the data down there — the spot that both File 1 and 2 are pointing to — both files change.
Even though they're separate files with different names, and are located in different places, changing the original data alters both files: that's how hard links work. Soft links work differently. Remember that the original file File 1 points to a specific spot on the hard drive, the spot where the data lives. And a hard link creates a new file that points to the same spot. When you create a symbolic link, however, it points to the File 1 descriptor or the File 1 name. A soft link of File 1 — we'll call it File 3 — only points to File 1's name.
It doesn't point to a spot on the hard drive. So, what does that mean? It means that File 2 takes up almost no space because it's nothing more than a pointer that goes to the original file. File 1 can be a 5TB file, but if you create a symbolic link File 3 it'll only point to that 5TB file, while remaining quite small. But there's a very important functional difference between soft links and hard links. Because a soft link points to the original file, if you delete File 1, File 2 will be completely useless, because it's going to be pointing to something that doesn't exist.
On the contrary, if you're working with hard links, which act as separate files that point to the same data on the hard drive and delete the original File 1, File 2 is still perfectly viable. File 2 keeps on pointing to the same spot on the hard drive where the data is stored. Delete a hard linked file and the data remains intact. But with a symbolic link, if you delete the original file, the symbolic link is rendered useless.
Now that we've got a conceptual understanding of hard links and soft links, let's get into a tangible understanding of them. If you have access to a Linux command line , we're going to walk through the commands for creating hard links and soft links. After we've created one of each, you'll probably see they're not terribly confusing once you see what's actually going on.
We're going to walk through a hypothetical file structure on our Ubuntu system. We start by opening up a terminal window, and looking at the files in our directory:. Like Article. Previous ln command in Linux with Examples. Next chroot command in Linux with examples. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment? Hard links are more flexible than soft links because you can access the file even if the original file is removed from the system.
A soft link refers to a symbolic path which indicates the location of the original file but not the contents. The In command is a standard UNIX command used to create a hard link to point to multiple files with the same name. Unlike soft links which can be used to link both files and directories, hard links can only link files but not directories. The difference gives soft links certain qualities that hard links do not have, such as the ability to link across the filesystem.
Hard links, on the other hand, cannot be used to link files outside the filesystem. In a nutshell, both soft links and hard links allow more than one file name to refer to the same file somewhere else, but the difference lies in the way they behave when the source link is removed from the system.
A soft link is like a shortcut feature of the Windows operating system used as an alias to refer to the original file or directory, whereas a hard link is like a replica of the original file that links two files in the same filesystem.
Difference Between Similar Terms and Objects. MLA 8 Khillar, Sagar. Name required. Email required. Please note: comment moderation is enabled and may delay your comment. There is no need to resubmit your comment. Notify me of followup comments via e-mail. Written by : Sagar Khillar.
0コメント