Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi guys, I am having a hard time understanding the concept of hard and soft links. I have been told that they are like Windows shortcuts, but what would be the practical implementation of hard and soft links? I understand how to create the links with the ln command but I’m not sure why the links are useful. Any help would be appreciated! Thanks
Hi guys, I am having a hard time understanding the concept of hard and soft links. I have been told that they are like Windows shortcuts, but what would be the practical implementation of hard and soft links? I understand how to create the links with the ln command but I’m not sure why the links are useful. Any help would be appreciated! Thanks
In short: Links allow you to organise data that resides in files. Without links, you have no directory tree. If a file has more than one link, it is organised in more than one way.
UNIX (and by extension Linux) has objects that are usually named "files". These objects exist on storage systems and are usually identified by an inode number.
It's very useful to also give those files a name. Interestingly, names don't reside in the structure that represents the file, such as the inode. UNIX separates names from the objects that are named. Names are stored in so-called directories, which are file objects of a special type. In UNIX terminology, such a name is called a "link", because it links the file object to a directory. While an inode doesn't contain the name(s) of the object it represents, it does count the links that currently exist for this object.
You now have the opportunity to link the same object into more than one directory, as long as those directories reside in the same filesystem. This is useful if you want to organise your data in more than one way.
When the directory is on a different filesystem than the file object, this doesn't work, since a directory entry only contains the name and inode number, not a reference to a different filesystem. To solve this problem, symbolic links were invented. They allow you to link an object across filesystems. Again, this makes it possible to organise your data in more than one set of categories.
The original links are also called hard links, and symbolic links are named soft links.
Just to confuse you a little more, there is also the concept of "bind mounts", which allow you to have a directory appear in several locations in the filesystem. Very similar, conceptually, to symbolic links. One difference compared to symbolic links: You can use bind mounts in a chroot environment.
The main differences between a windows (microsoft) shortcut and a link are:
1. Deleting a linked file does not delete the file. It only reduces the linked count by one. This seems reasonable and as expected.
2. When you right click and copy a linked file, you are infact making a copy of the file. whereas in windows, you are only lf. This copying the reference and not the file itself. This is not expected behaviour. It is a common mistake.
putting it succinctly two parts to a file and data, hard links have same inode as as actual data they point to in the file they are linked to.If the original file gets deleted the hard link will still have access to original data.
A soft link points to a file, when that file the soft link is pointing to is deleted, then soft link is unable to access data.
The main differences between a windows (microsoft) shortcut and a link are:
1. Deleting a linked file does not delete the file. It only reduces the linked count by one. This seems reasonable and as expected.
When the link count drops to 0, the file is deleted.
And deleting a Windows shortcut only deletes the shortcut. Very similar to a symbolic link in Linux.
Quote:
2. When you right click and copy a linked file, you are infact making a copy of the file. whereas in windows, you are only lf. This copying the reference and not the file itself. This is not expected behaviour. It is a common mistake.
The file managers that I know behave differently for symbolic links (the link is copied) and hard links (the link is copied to the same file system, a new file is created when copying to a different filesystem).
The Microsoft people seem to have been envious of the power of links in Linux because they have recently tried to create their own version of it. Of course all they have done is to create an almighty mess! They call it a "library": a collection of files grouped in a way that has no relationship at all to the underlying directory structure. I think it's done by creating directories that contain only symbolic links to files stored elsewhere.
The difference is that in Linux, symbolic links are transparent. You can always see what file a link actually points to and where it is. Just use the ls command with the -l (long) qualifier and each symbolic link will show a pointer to its target. In the Windows version, the actual directory/file structure is completely hidden. You only see the artificial one, the "library" created by the links. So if those get broken (and they do!), you can't find out where the files actually are. A friend of mine nearly lost his photograph collection when his library links stopped working.
In windows, if you delete the file, the shortcut(s)are useless. OTOH, in linux, if you delete the file, the file lives by the linked name.
That depends on whether it's a hard or a soft link. If it's a soft or symbolic link, deleting the file makes the link useless because what it points to no longer exists. Just like a Windows shortcut, in fact. If it's a hard link, deleting the original name won't do any harm because the actual file content won't be deleted if there is another name linked to it.
Quote:
AFAIR, in linux, you can also have different permissions on each linked name.
You can if it's a symbolic link, because those are files in their own right. But hard links are just names that point to the same inode. The permissions are stored in the inode, so they'll be the same for both names.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.