0

Suppose I have the following configuration:

  • Folder A
    • file1
    • file2

I then create a soft link of Folder A as follows: ln -s A B so that we now have:

  • Folder A
    • file1
    • file2
  • Folder B ( symlink of Folder A (softlink) )
    • file1
    • file2

Now when I change B/file1, that change propogates to A/file1. However, it appears that - when using ls -l in B - that B/file1 and B/file2 are not soft linked to A/file1 and A/file2! What is going on?

Question: Is it true that B/file1 is hard linked to A/file1? Another way of asking this: is it true that when you soft link a directory, that its subfiles become hard links to the subfiles of the original directory? Is there a way to make everything a soft link?

2 Answers 2

3

They're not hard links for two reasons:

  1. The count of hard links for files in soft linked directories as presented via ls is 1. If the files were hard linked, the count would be 2 or more.
  2. Hard links can't span filesystems while soft links can. If hard links were to be created, then you couldn't soft link directories across filesystems.

What you're really seeing is just another way of getting to the same directory from different paths. Once you're in the directory or operating on the files in them, they're the exact same files (think adding a door to a room, the contents don't change, but there's a new way to get in).

If you wanted the contents of the directory to be soft links, then you'd need to make soft links for each file in the directory rather than the directory itself, but that would make directory listings a bit messier. Aside from having different names for the sources and targets I'm not sure what advantage you'd get out of it.

3

It seems that you are confusing things.

Symbolic link: A symbolic link just pointer to another path in the system. That path can exist or not, that doesn't matter, it can also be absolute or relative. A symbolic link has nothing to do with the target file, it just points. It can even be on another physical disk. It can be compared to a hyperlink on a website.

Hardlink: A hardlink is an assignment of a name to a file (precisely the inode number of the file). This relation is called surjective. Every file has at least one hardlink refering to its inode number. The inode number is the reference to the file. Modern filesystems have therefore no fix assignment of filename and the actual file.

If a file has 2 hardlinks refering to its inode and one of them is deleted (by the unlink() system call), just that hardlink is removed, the file and the inode stay untouched (that's why the system call is named unlink() not delete()). As soon as the number of hardlinks decreases to zero, the inode is finally removed and thus the file.


So that was the theory.

When you change B/file1 then A/file1 is changed too, because it's the same hardlink refering the same inode number refering the same file. You just went over the symbolic link A pointing to directory B, instead of the real directory B.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .