Skip to content
Advertisement

hard links refererring to a file ant stat st_nlink don’t match

Using Ubuntu 18.04 bash, if I list all files that share the same specific inode 4 with:

sudo find -inum 4 -printf "%D %i %n %pn" 2>/dev/null

I can see different values of the number of hard link for that same specific inode (=4). The same occur if I do it with C code.

For other inodes I get the correct identical hard links values.

What is the problem with that inode 4?

Here is the output:

54 4 2 d ./run/user/1000/systemd
52 4 2 d ./run/user/121/systemd
27 4 1 f ./run/lock/asound.state.lock
23 4 7 d ./run/udev
66305 4 4 d ./boot/efi/EFI
6 4 1 c ./dev/rfkill
8 4 1 f ./sys/kernel/debug/pinctrl/pinctrl-devices
21 4 23 d ./sys/devices
43 4 1 f ./sys/fs/cgroup/devices/cgroup.sane_behavior
42 4 1 f ./sys/fs/cgroup/perf_event/cgroup.sane_behavior
41 4 1 f ./sys/fs/cgroup/hugetlb/cgroup.sane_behavior
40 4 1 f ./sys/fs/cgroup/freezer/cgroup.sane_behavior
39 4 1 f ./sys/fs/cgroup/net_cls,net_prio/cgroup.sane_behavior
38 4 1 f ./sys/fs/cgroup/pids/cgroup.sane_behavior
37 4 1 f ./sys/fs/cgroup/cpuset/cgroup.sane_behavior
36 4 1 f ./sys/fs/cgroup/cpu,cpuacct/cgroup.sane_behavior
35 4 1 f ./sys/fs/cgroup/blkio/cgroup.sane_behavior
34 4 1 f ./sys/fs/cgroup/memory/cgroup.sane_behavior
33 4 1 f ./sys/fs/cgroup/rdma/cgroup.sane_behavior
30 4 1 f ./sys/fs/cgroup/systemd/cgroup.sane_behavior
29 4 1 f ./sys/fs/cgroup/unified/cgroup.controllers
1812 4 1 l ./snap/core18/1074/bin/bzcmp
1797 4 1 l ./snap/core18/1066/bin/bzcmp
1803 4 1 f ./snap/gnome-characters/292/flavor-select
1809 4 1 f ./snap/gnome-characters/296/flavor-select
1810 4 2 d ./snap/gnome-3-28-1804/67/etc/X11/Xreset.d
1794 4 2 d ./snap/gnome-3-28-1804/63/etc/X11/Xreset.d
1796 4 25 d ./snap/gtk-common-themes/1313/share/gtk2
1804 4 15 d ./snap/gtk-common-themes/1198/share/gtk2
1798 4 1 f ./snap/gnome-system-monitor/100/flavor-select
1801 4 1 f ./snap/gnome-system-monitor/95/flavor-select
1807 4 1 l ./snap/core/7169/bin/bzcmp
1808 4 1 l ./snap/core/7270/bin/bzcmp
1806 4 1 f ./snap/gnome-calculator/260/flavor-select
1802 4 1 f ./snap/gnome-calculator/406/flavor-select
1805 4 1 f ./snap/gnome-calculator/352/flavor-select
1793 4 3 d ./snap/gnome-logs/45/etc
1792 4 1 f ./snap/gnome-logs/61/flavor-select
1800 4 1 f ./snap/gnome-logs/57/flavor-select
1799 4 2 d ./snap/gnome-3-26-1604/90/etc/X11/Xreset.d
1795 4 2 d ./snap/gnome-3-26-1604/88/etc/X11/Xreset.d

Advertisement

Answer

After editing, the output of the modified command shows that the inode number is unique per file system. With different device numbers, the number of hard links is not related to each other.

As expected you see a link count of 1 for normal files (type f) or devices (type c for a character device in this case).

Directories (type d) have a link count of at least 2 for the directory name and the contained .. If the directory contains subdirectories, the link count will be equal to the number of immediate subdirectories + 2, because every subdirectory will contain a .. entry.

For information about /snap see https://snapcraft.io/docs/system-snap-directory

For information about /sys/fs/cgroup see http://man7.org/linux/man-pages/man7/cgroups.7.html

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement