LinuxQuestions.org
Visit Jeremy's Blog.
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 04-24-2021, 12:41 AM   #1
hacback17
LQ Newbie
 
Registered: Nov 2019
Posts: 12

Rep: Reputation: Disabled
Understanding Hard Links in Linux


An online instructor said 119 is the hard link count (shown in the attached screenshot). So what does that mean? Does it mean there are other 118 hard link that point to this directory (i..e. /etc)? If there are, where and how I can find them?

EDIT: Now I could find one more hard link:
Code:
[tux@server1 ~]$ ls -ldhi /etc /etc/.
16777281 drwxr-xr-x. 119 root root 8.0K Apr 23 18:52 /etc
16777281 drwxr-xr-x. 119 root root 8.0K Apr 23 18:52 /etc/.
Now I am wondering where the other 117 hard link are that are pointing to the same inode?
Attached Thumbnails
Click image for larger version

Name:	Screenshot 2021-04-24 111037.png
Views:	23
Size:	32.3 KB
ID:	36217   Click image for larger version

Name:	Screenshot 2021-04-24 111352.png
Views:	17
Size:	13.5 KB
ID:	36218  

Last edited by hacback17; 04-24-2021 at 12:47 AM.
 
Old 04-24-2021, 03:56 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
I think you found the solution. ls -i will display inode numbers and you [only] need to find now that inode (16777281) 119 times. You know it is a directory, so you do not need to check files. But I will give you additional help:
every /etc/<whatever>/.. are the same inode. I don't know how many subdirs do you have in /etc (probably 117?).
 
1 members found this post helpful.
Old 04-24-2021, 07:24 AM   #3
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
Mainly protect against accidental removal of important file. With non-zero number of hard links file can't be removed. The first one needs to remove links. Hard links can't go beyond partition.
 
Old 04-24-2021, 07:52 AM   #4
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,682
Blog Entries: 19

Rep: Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492
Programs also make temporary hard links to files that they open, either to read data from them or to link to them as libraries. Deleting such a file will remove its name from the relevant directory, but the file contents won't be lost as long as there are existing hard links to it. That explains how Linux, unlike Windows, can update libraries on the fly without the need to reboot. Any programs already using that library can go on using it until they exit. Meanwhile the name transfers to the new file, and any newly-launched programs will link to that.
 
1 members found this post helpful.
Old 04-24-2021, 08:04 AM   #5
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,347

Rep: Reputation: Disabled
Quote:
Originally Posted by hazel View Post
Programs also make temporary hard links to files that they open, either to read data from them or to link to them as libraries.
Are you referring to the file descriptors in /proc/<pid>/fd?

I don't think they really qualify as hard links, but boy have they saved my behind on a few occasions.
 
Old 04-24-2021, 08:41 AM   #6
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,682
Blog Entries: 19

Rep: Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492Reputation: 4492
Quote:
Originally Posted by Ser Olmy View Post
Are you referring to the file descriptors in /proc/<pid>/fd?
Those are for files being accessed for read/write. But there are also the dynamically-linked libraries opened by dlopen() calls. I believe that both kinds are registered in the link count field in the file's inode and that neither the inode nor the data blocks will be recycled as long as there is a non-zero value in that field.
 
Old 04-24-2021, 09:15 AM   #7
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,003

Rep: Reputation: 474Reputation: 474Reputation: 474Reputation: 474Reputation: 474
Hard links are used to implement a directory's self (.) and parent (..) pointers. A directory containing N subdirectories will have at least N+1 hard links (N+2 including the link in the parent's directory).

Hard links have only a few good uses.
Ed

Last edited by EdGr; 04-24-2021 at 09:23 AM.
 
Old 04-24-2021, 09:23 AM   #8
Emerson
LQ Sage
 
Registered: Nov 2004
Location: Saint Amant, Acadiana
Distribution: Gentoo ~amd64
Posts: 7,665

Rep: Reputation: Disabled
rsnapshot is using hard links and it saves enormous amount of space occupied by backups. I use hard links when I want some file to appear in several places, often with different name. Maybe it is only "a few good uses" ... for me it is plenty.
 
Old 04-24-2021, 09:50 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
You can systematically find the hardlinks like this:

Get the inode number, 1st column in
Code:
ls -ldi /etc
Say it is 16777281

Get the file system root dir from (mounted on)
Code:
df /etc
Say it is /

Then search the inode number in the file system
Code:
find / -xdev -inum 16777281 | cat -n
 
Old 04-24-2021, 10:13 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
Quote:
Originally Posted by hazel View Post
Programs also make temporary hard links to files that they open, either to read data from them or to link to them as libraries. Deleting such a file will remove its name from the relevant directory, but the file contents won't be lost as long as there are existing hard links to it. That explains how Linux, unlike Windows, can update libraries on the fly without the need to reboot. Any programs already using that library can go on using it until they exit. Meanwhile the name transfers to the new file, and any newly-launched programs will link to that.
These references exist in (kernel-)memory. They are not called hard links.

Hard links are written into the file system (directories).

Yes, Linux keeps its dynamic libraries opened; you can check that with lsof.
Unlike Unix that closes the handle when a dynamic library is loaded.
That is why Linux defaults to 1024 file handles per process: it might need a few hundreds for the dynamic libraries.
And Unix often defaults to 256 file handles. (Of course it is tunable.)
 
1 members found this post helpful.
Old 04-27-2021, 12:44 AM   #11
mrmazda
LQ Guru
 
Registered: Aug 2016
Location: SE USA
Distribution: openSUSE 24/7; Debian, Knoppix, Mageia, Fedora, others
Posts: 5,878
Blog Entries: 1

Rep: Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078Reputation: 2078
Quote:
Originally Posted by Emerson View Post
rsnapshot is using hard links and it saves enormous amount of space occupied by backups. I use hard links when I want some file to appear in several places, often with different name. Maybe it is only "a few good uses" ... for me it is plenty.
MeeToo!

Also, file timestamps in directory lists are important to me. Creation date of a symlink tells me nothing I care about. A hardlink provides the answer I want.
 
Old 04-27-2021, 12:50 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
In general hardlinks on directories are not allowed, only the system can do that (for . and .. and a probably in a few other cases).

Quote:
Originally Posted by Emerson View Post
rsnapshot is using hard links and it saves enormous amount of space occupied by backups.
That is not a backup, just another directory entry to the very same file. Cannot be used to restore if the original file was corrupted/damaged.
 
  


Reply

Tags
hard links, inode



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: Understanding the Differences Between Soft and Hard Links in Linux LXer Syndicated Linux News 0 02-22-2020 02:03 AM
LXer: Understanding Hard and Soft Links on Linux LXer Syndicated Linux News 0 09-09-2019 06:56 PM
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
web page/links links/links vendtagain Linux - Newbie 2 09-19-2009 08:13 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 09:09 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