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 02-09-2017, 10:34 AM   #1
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Rep: Reputation: 56
List all hard links related to inode...


Hello

I want to find all the hard links that are related to particular inode number.

For example, I have this directory structure ./test ./test/dir1 ./test/dir2 ./test/dir3

Code:
# ls -ld test/
drwxr-xr-x. 5 root root 42 Feb  9 21:42 test

# ls -l test/
drwxr-xr-x. 2 root root 6 Feb  9 20:37 dir1
drwxr-xr-x. 2 root root 6 Feb  9 20:37 dir2
drwxr-xr-x. 2 root root 6 Feb  9 21:42 dir3
From some papers on Linux that i received, the author of that papers tells how to find the number of immediate sub directories in a directory easy way. He explains to look for number of hard links in a directory listing and subtract it from 2, which will give the number of immediate subdirectories in it.

On why subtract by 2, he explains the inode for the directory itself and the . file in that directory which represent current directory, refer to same inode. So less 2.

So in my example, the count is 5, that gives me 3 sub directories, which is correct.

Code:
# ls -ldi test test/.
67395691 drwxr-xr-x. 5 root root 42 Feb  9 21:42 test
67395691 drwxr-xr-x. 5 root root 42 Feb  9 21:42 test/.
And since each subdirectory has .. file in them that link to parent directory, ie., test directory in this case, which counts for that remaining 3.

Code:
# ls -ldi test/dir1/.. test/dir2/.. test/dir3/..
67395691 drwxr-xr-x. 5 root root 42 Feb  9 21:42 test/dir1/..
67395691 drwxr-xr-x. 5 root root 42 Feb  9 21:42 test/dir2/..
67395691 drwxr-xr-x. 5 root root 42 Feb  9 21:42 test/dir3/..
Cool. So far so good. Now that i know there is inode 67395691, i want to see al the hard links attached to it.

I tried

Code:
# find ./ -inum 67395691
./test
but it only points to ./test dir. I want to see all the links that are pointing to that inode, like

Code:
67395691 drwxr-xr-x. 5 root root 42 Feb  9 21:42 test
67395691 drwxr-xr-x. 5 root root 42 Feb  9 21:42 test/.
67395691 drwxr-xr-x. 5 root root 42 Feb  9 21:42 test/dir1/..
67395691 drwxr-xr-x. 5 root root 42 Feb  9 21:42 test/dir2/..
67395691 drwxr-xr-x. 5 root root 42 Feb  9 21:42 test/dir3/..
How do I achieve that?

Sorry for long illustration.

Thanks in advance.
 
Old 02-09-2017, 10:38 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Easy question. hardlinks cannot be directories. So the one file is the only one used by that inode.
 
1 members found this post helpful.
Old 02-09-2017, 10:42 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
You can go by the name of the directory to find, by name, the subdirectories and use stat to print the inode:

Code:
find ./test/ -type d -exec stat --printf="%i\t%n\n" {} \;
 
Old 02-09-2017, 12:15 PM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
for directories there are no hardlinks, the only exception is what you listed.
So:
dir
dir/.
dir/subdir1/..
dir/subdir2/..
dir/subdir3/..
dir/subdir4/..
dir/subdir5/..
dir/subdir6/..
...
you only need to list the subdirs
find by default does not list . and ..

actually see man find, and look for -noleaf
 
1 members found this post helpful.
Old 02-10-2017, 07:18 AM   #5
ddenial
Member
 
Registered: Dec 2016
Distribution: CentOS, Fedora, Ubuntu
Posts: 359

Original Poster
Rep: Reputation: 56
Sorry for delayed response. So basically hard link cannot be created on directory. I didn't knew that. Then searching for what other directories are connected to that inode doesn't make sense.

Thanks.
 
Old 02-10-2017, 07:55 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
In several parts of the system there is the tacit assumption that a filesystem directory structure is a tree. Making arbitrary hard links to a directory breaks that. In the distant past in Unix, it was possible for the root user to make hard links to a directory**. Anyone who tried that (raises hand, sheepishly) ended up with a broken filesystem and had to resort to the low-level filesystem debugger to repair it.
** The rename command had to do that to rename directories, since there was no rename(2) system call at the time.
 
Old 02-10-2017, 08:00 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
just two additional comments:
1. hard links on directories are possible, not only theoretically, but "in real filesystems" too, just it is disabled.
2. using other kind of filesystem(s) I found hard links among dirs and that caused really strange errors. So better to not use them....
 
  


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
Why does vm_area_struct have both rb tree node links and linked list links? dlevy022 Linux - Kernel 1 12-22-2015 07:48 PM
Hard links and soft links - the esoteric details tekra Linux - General 2 10-09-2014 01:15 AM
How to know or to list all the soft and hard links an installed distribution has ! Don-X Linux - Software 1 07-07-2011 06:05 PM
hard links problem: how to delete all pointers to an inode at once? onufry Linux - General 6 11-18-2007 06:27 PM
ext2/ext3 limit of 32,000 links in an inode ogross74 Linux - General 2 10-10-2005 12:52 PM

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

All times are GMT -5. The time now is 01:23 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