LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   List all hard links related to inode... (https://www.linuxquestions.org/questions/linux-newbie-8/list-all-hard-links-related-to-inode-4175599369/)

ddenial 02-09-2017 10:34 AM

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.

Turbocapitalist 02-09-2017 10:38 AM

Easy question. hardlinks cannot be directories. So the one file is the only one used by that inode.

Turbocapitalist 02-09-2017 10:42 AM

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" {} \;

pan64 02-09-2017 12:15 PM

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

ddenial 02-10-2017 07:18 AM

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.

rknichols 02-10-2017 07:55 AM

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.

pan64 02-10-2017 08:00 AM

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....


All times are GMT -5. The time now is 02:36 PM.