LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Why are hard links not created in directories? (https://www.linuxquestions.org/questions/linux-newbie-8/why-are-hard-links-not-created-in-directories-4175542849/)

anishas 05-17-2015 01:15 PM

Why are hard links not created in directories?
 
Hi All,

Iam Anisha S.

I would like to know why hard links are not created for directories?

Thanks,
Anisha.

pan64 05-17-2015 01:27 PM

actually they exist, for example the . and .. are both hardlinks. But in general it is not allowed because that may cause strange "side" effects (just imagine - for example - a simple find or ls -r or grep -r may never end).

anishas 05-17-2015 01:43 PM

Hello Pan64,

Thanks for the reply.
Can you please explain me little more clearer.

As per my understanding, Soft links are like shortcuts with different inode number to that of the original file.

But whereas ,in a hardlink the inode numbers are same to the original file and deleting the original file doesnt affect it.Its just they cannot be shared across filesytem.

In that case why can t we create a hardlink for directory ?? It will replicate the original directory correct ?

Thanks,
Anisha.

pan64 05-17-2015 01:54 PM

https://en.wikipedia.org/wiki/Hard_link

gengisdave 05-18-2015 07:12 AM

you can't hard link an inode which represent a directory to avoid infinite loop in the directories tree

Code:

cd a
mkdir b
cd b
ln ../a c

you'll have an infinite path a/b/c/b/c/b/c/b and so on

this behaviour happens with symlinks, and every (decent) program ignores to follow symlinks in deep searches

jpollard 05-19-2015 09:15 AM

The problem occurs when the filesystem gets repaired.

The directory hierarchy is designed to represent a tree. A directory contains hard links to directories contained within that directory. Each directory has a hard link to its parent.

When you allow for a hard link from elsewhere, you create errors in that tree - it is no longer a tree but a network. Analysing a network is much harder than a simple tree - and can take forever (depending on how the network may loop back on itself) and could make recovery impossible.

anishas 05-19-2015 09:57 AM

Hi jpollard/gengisdave,

Thanks for the info ..It was clear ..


All times are GMT -5. The time now is 10:36 AM.