LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 10-15-2019, 07:56 PM   #1
Joshie
LQ Newbie
 
Registered: Oct 2019
Posts: 1

Rep: Reputation: Disabled
Purpose of links in Linux


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
 
Old 10-15-2019, 08:11 PM   #2
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,321
Blog Entries: 28

Rep: Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141
Here's a good introduction: https://www.geeksforgeeks.org/soft-h...nks-unixlinux/
 
1 members found this post helpful.
Old 10-15-2019, 10:17 PM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by Joshie View Post
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.
 
1 members found this post helpful.
Old 10-16-2019, 04:34 AM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
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.

OK

Last edited by AnanthaP; 10-16-2019 at 04:36 AM.
 
Old 10-16-2019, 05:00 AM   #5
captain_sensible
Member
 
Registered: Apr 2010
Posts: 352

Rep: Reputation: 145Reputation: 145
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.
 
Old 10-16-2019, 05:03 AM   #6
captain_sensible
Member
 
Registered: Apr 2010
Posts: 352

Rep: Reputation: 145Reputation: 145
so you can create simple text file and create links to them such as:
ln original.txt mylink.txt

(hard link is default)

delete original.txt and cat mylink.txt

data will show try ln -s orgiginal2.txt mylink2.txt , delete original2.txt and cat mylink2.txt
 
Old 10-16-2019, 05:07 AM   #7
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by AnanthaP View Post
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).
 
Old 10-16-2019, 05:16 AM   #8
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,572
Blog Entries: 19

Rep: Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451
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.

Last edited by hazel; 10-16-2019 at 05:17 AM.
 
2 members found this post helpful.
Old 10-16-2019, 05:17 AM   #9
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Quote:
And deleting a Windows shortcut only deletes the shortcut. Very similar to a symbolic link in Linux.
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.


OK

Last edited by AnanthaP; 10-16-2019 at 05:24 AM.
 
Old 10-16-2019, 05:27 AM   #10
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,572
Blog Entries: 19

Rep: Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451Reputation: 4451
Quote:
Originally Posted by AnanthaP View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] What is the purpose of lib64? How does it serve it's purpose? BMan8577 Linux - Newbie 2 09-20-2011 01:39 PM
web page/links links/links vendtagain Linux - Newbie 2 09-19-2009 08:13 PM
What's the purpose of links in a filesystem that go nowhere? ayteebee Linux - General 7 09-08-2006 08:10 PM
links (hard links and soft links..) sachitha Programming 1 08-10-2005 12:10 PM
Links Section updated - Please add your Linux links. jeremy Linux - General 2 11-24-2001 11:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration