LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-12-2019, 08:20 AM   #1
Portal
LQ Newbie
 
Registered: Jun 2019
Posts: 27

Rep: Reputation: Disabled
inode structure of soft links vs hard links


Inode numbers are the same for hard links. How does this help me access the contents of the link after deleting the original file? Why is this different for soft links, where the inode numbers are different? I know how the inode structure works, but I still can't figure it out...
 
Old 07-12-2019, 08:38 AM   #2
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,565
Blog Entries: 19

Rep: Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446
I hope this isn't a homework question! Here's a clue: one of the fields in the inode is a hard link counter. When a filename is deleted, the corresponding inode and data blocks are only marked for recycling if the link count has dropped to zero. So if there is a hard link to another filename...

A soft link is just a file which references another file by name, not by inode. Remove the target and you just get lost data and a broken link.
 
1 members found this post helpful.
Old 07-12-2019, 08:42 AM   #3
Portal
LQ Newbie
 
Registered: Jun 2019
Posts: 27

Original Poster
Rep: Reputation: Disabled
oh thanks!
 
Old 07-12-2019, 08:51 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
When a file has multiple hard links, there is no notion of which is the "original" link. When you delete one of those links, that is all that happens. The other links remain valid. The file is not actually deleted (inode marked "deleted" and space released) until the link count in the inode goes to zero and no process currently has the file open.

A soft link is just a tiny file that contains instructions (a path) for reaching the target. It does not contribute to the link count in the target's inode, and indeed there is no requirement that the target actually exist. If the target file is deleted, the soft link (now broken) remains. To add to the confusion, note that a soft link is just a specially marked file and can itself have multiple hard links.
 
Old 07-12-2019, 12:03 PM   #5
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
Originally Posted by rknichols View Post
and no process currently has the file open.
A very important point. Often I see the question "Why didn't the space get freed up when I deleted the file?" The reason is because the file NAME got deleted but not the file INODE because the file was still open by some process. 90% of the time it is a log file that was still being written to.

One of the beauties of lsof is it can show PIDs with large regular files open even when the actual file NAME is no longer there. Killing the PID (cleanly of course) closes the file and frees up the space. Until lsof became available one would have to reboot to kill all the PIDs and thereby free up mystery space.
 
1 members found this post helpful.
Old 07-12-2019, 01:06 PM   #6
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,565
Blog Entries: 19

Rep: Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446Reputation: 4446
This also explains how Linux can do software updates without requiring the user to reboot afterwards. In Windows, a library can't be deleted while in use; all that can be done is to flag it for deletion and store the replacement file somewhere. The switch is made when you shut down.

In Linux, the library can be deleted and the new one installed because all you're deleting is the name. The inode and data blocks remain available to any running program linked to that library. Only libraries not currently in use are completely deleted and replaced.
 
  


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
LXer: Hard Links vs Soft (Symbolic) Links in Linux and How to Create Them LXer Syndicated Linux News 0 04-19-2019 02:03 AM
Hard links and soft links - the esoteric details tekra Linux - General 2 10-09-2014 01:15 AM
Choosing between soft links and hard links. tekra Linux - General 6 01-03-2013 07:36 PM
technical question about hard links v soft links 7stud Linux - Newbie 2 02-23-2007 06:57 PM
links (hard links and soft links..) sachitha Programming 1 08-10-2005 12:10 PM

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

All times are GMT -5. The time now is 02:59 AM.

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