3

My understanding of the unix/inode file system is that two names can reference the same inode number, but that in such cases the reference count would be 2. However, I noticed today that the /etc/hosts file and the /private/etc/hosts file both reference the same inode number, but the reference count is 1. Why isn't the reference count 2, reflecting the fact that two paths are referencing the same inode?

% ls -li /etc/hosts
38402042 -rw-r--r--  1 root  wheel  236 May 14 23:13 /etc/hosts

% ls -li /private/etc/hosts
38402042 -rw-r--r--  1 root  wheel  236 May 14 23:13 /private/etc/hosts
Tim
  • 365
  • 2
  • 15
  • 1
    mmmh. If `/private/etc` is a link to `/etc` the hosts file is a single object instead of two pointing to the same disk location. Could that be the case? – Ronald May 15 '14 at 06:53
  • ok, it's the other way around, but basically the same. There's only one directory entry `/private/ete/hosts` – Ronald May 15 '14 at 07:20
  • It looks like the directory /etc is soft linked to /private/etc as you suggest. `% ls -l /etc lrwxr-xr-x@ 1 root wheel 11 Mar 17 2011 /etc -> private/etc`. If you ln -s existing_dir new_directory you create a new inode for the new_directory (symbolic links create a new inode to hold the forwarding path information), but since it is a directory, all of the contents of the existing directory get forwarded from the new_directory. This creates two paths with the same inode number, but a reference of 1. If you rm the file from either location it is gone! – Tim May 15 '14 at 07:26
  • 2
    What I mean is, that `/etc` is an alias (sym link) for `/private/etc`. Thus any files you _see_ in `/etc` are actually files in `/private/etc`. Therefor the same inode and a link count of 1 for all files and directories if you compare the contents of `etc` and `/private/etc`. Oh, and don't delete them. That would lead to a lot of work. – Ronald May 15 '14 at 07:34
  • so you can rm a hard link with a reference count of 2 and the other reference survives (the inode still exists). But rm a symbolic link delete the inode (goodbye file). If you simply want to remove the link and leave the file, you should use unlink. – Tim May 15 '14 at 07:43
  • yes, if you rm a file with a link count of 2 (it is hard linked), the other reference remains. If you rm a soft link, the alias vanishes. In this case it would be a terrible idea, because you would remove `/etc` which would break your system. I don't know why you have this situation, maybe you ask your sysadmin for a reason. Actually the rm command does nothing more than unlink. The file system removes the file as soon as the link count goes zero. – Ronald May 15 '14 at 07:47

2 Answers2

0

Hard links share the same inode and hard link is more of a mirror copy.
Hard Link syntax: ln file1 file2
if you deleted file1 and it remains the file2.

Ayyappa Boligala
  • 1,096
  • 7
  • 7
0

I would suggest that you use namei (https://linux.die.net/man/1/namei) to find out ALL symlinks in the path to that file. In one of your comments, you stated etc is a symlink to private/etc. If you use namei on /etc/hosts, it would reveal all the paths all the way to the file, including the symlinks in between.

ZeZNiQ
  • 612
  • 7
  • 14