0

This page on inodes has been exceptional help in grasping the surface-level concept of file systems. On the same page, the author has inserted this snippet demonstrating that each file or directory has at least 2 names (and hard links):

/tmp/junk$ ls -id ..
327681 ..
/tmp/junk$ cd ..
/tmp$ ls -id .
327681 .

We can see that /tmp has 3 hard links:

  1. Presumably, an inode for the filename “tmp”
  2. The same inode for the name “..”
  3. The same inode for the name “.”

My question: can the “junk” file in the /tmp directory also have 3 names (and hard links) if it is given a child directory? For example, /tmp/junk/paper_balls.

My hypothesis: If the “junk” file becomes a parent, it can be invoked with .. but relatively, meaning the current working directory (from which .. is typed) would have to be within the directory path /tmp/. The answer to my question is probably too advanced.

  • Hard link count of a directory is not reliable. It's filesystem dependent. Technically, hard link count for any inode is meaningless, you don't need to care about it, it's simply a have-to for most filesystem to work. – 炸鱼薯条德里克 Sep 23 '19 at 22:50
  • @炸鱼薯条德里克 I’m using the Stack Exchange app, and it’s pretty buggy. Really? I was starting to see magic. The second field in the result of ls -l tells you the hard link count. It may be relatively meaningless information, but it’s nice to be able to read command results like a name badge. I was thinking each inode created a hard link. – Steven Sep 23 '19 at 22:57
  • Can't you run a simple experiment to find out? mkdir junk; cd junk; ls -lid .;mkdir foo; ls -lid .; mkdir bar; ls -lid .; mkdir baz; ls -lid . should clarify what is happening. – NickD Sep 23 '19 at 23:08
  • Can't really understand your language. Inode is just inode, a hard link is a name for it. You can create a inode without name – 炸鱼薯条德里克 Sep 23 '19 at 23:14
  • Maybe this can help: askubuntu.com/a/1073803/295286 – Sergiy Kolodyazhnyy Sep 24 '19 at 4:26
3

The initial number of hard links is 1 for a file and 2 for a directory (the first link is its name in the parent folder, and the second hard link is .). The link count for a directory goes up by one each time a subdirectory is created in it (due to .. in each subdirectory).

This count can be easily viewed with ls -l. It is the second value. Take a look:

~/x$ ls -la
total 16
drwxr-xr-x  2 tomasz tomasz 4096 Sep 24 00:08 .
drwxr-xr-x 54 tomasz tomasz 4096 Sep 24 00:11 ..
-rw-r--r--  1 tomasz tomasz   19 Sep 23 18:45 1
-rw-r--r--  1 tomasz tomasz   19 Sep 23 18:45 2
~/x$ mkdir d
~/x$ ls -la
total 20
drwxr-xr-x  3 tomasz tomasz 4096 Sep 24 00:11 .
drwxr-xr-x 54 tomasz tomasz 4096 Sep 24 00:11 ..
-rw-r--r--  1 tomasz tomasz   19 Sep 23 18:45 1
-rw-r--r--  1 tomasz tomasz   19 Sep 23 18:45 2
drwxr-xr-x  2 tomasz tomasz 4096 Sep 24 00:11 d
~/x$ mkdir d/dd
~/x$ ls -la
total 20
drwxr-xr-x  3 tomasz tomasz 4096 Sep 24 00:11 .
drwxr-xr-x 54 tomasz tomasz 4096 Sep 24 00:11 ..
-rw-r--r--  1 tomasz tomasz   19 Sep 23 18:45 1
-rw-r--r--  1 tomasz tomasz   19 Sep 23 18:45 2
drwxr-xr-x  3 tomasz tomasz 4096 Sep 24 00:11 d

The second value for d went up from 2 to 3 after creating d/dd within it.

See mosvy's comments below for a wider view.

  • Oh, are hard links unique per directory path? So, if I run ls -l on two separate directory paths, will I get different values on the 2nd field? I need to start a shell session! The page I read from, which is linked above with the question, said, “Inodes are always unique, but unique per [disk] partition.” – Steven Sep 23 '19 at 22:33
  • @Steven They are underneath paths, and so the count should be the same wherever you count. See this about partitions: unix.stackexchange.com/questions/290525/… – user147505 Sep 23 '19 at 22:59
  • @Steven If you are interested with links then go as deep as you want. Otherwise, it's enough to remember there are some entities called inodes on which the files and directories are based. You'll meet them now and then but not so often. Possibly it's a better idea to start with symbolic links, which are more popular and higher level. – user147505 Sep 23 '19 at 23:23
  • No. A directory can have a link count of 1 (on filesystems where directories don't have physical . and .. entries, and where its link count will stay 1 when subdirs are created), or even 0 (for a deleted directory which is still the cwd of some process or is still open from somewhere). – mosvy Sep 24 '19 at 6:21
  • 1
    @Tomasz if you're on debian like, this will set up everything apt-get install btrfs-progs && truncate -s 3G /tmp/3G && mkfs.btrfs -f /tmp/3G && mkdir -p /tmp/btrfs && mount /tmp/3G /tmp/btrfs and you could then happy-testing inside /tmp/btrfs ;-) – mosvy Sep 24 '19 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.