Linking Files: Hard Link vs. Soft Link?

Kyle Smith kyle.smith at
Tue Apr 14 18:53:02 UTC 2009

Smoot Carl-Mitchell wrote:
> On Tue, 2009-04-14 at 19:45 +1200, Tim Frost wrote:
>>> Is it possible to create a hard link to a directory somehow?
>> No. In general, a directory is a container, and as such it defines a
>> place in the tree which holds files (and other directories).  That
>> container only makes sense in that one place in the tree.
> Hardlinks to directories are not allowed, except when creating a
> directory.  A hard link is created to a new directory's parent directory
> with the name ".." and linked to itself with the name ".".  This
> standard is the way the utilities walk up the filesystem hierarchy.  The
> "/" directory has the ".." hardlink point to itself.
> This is the only case where hardlinks to directories appear in the
> filesystem hierarchy.  Some Unices do allow userland programs to create
> directory hardlinks, but it is inadvisable to do this, since many of the
> standard utilities depend on this not being the case.  You can see the
> hardlink count with the ls -la command. The -a lets you see the
> directory links and all the hidden files in a directory.  The link count
> is the number after the permissions.  A directory with lots of
> subdirectories will have a lot of hardlinks.  Adding "-i" as an option
> will let you display the inode number which is the only unique
> identifier for each file.
Apparently you all haven't had the pleasure of working with cPanel's WHM 
product.  WHM uses something called virtfs to create chroot environments 
for users to SSH into, and for SuEXEC PHP scripts to run inside.  What's 
fantastic about how they do it is that they hardlink the /home/username 
directory inside /home/virtfs/username/home/, and they hardlink the /lib 
directory in /home/virtfs/username/lib.  What's especially great about 
this is that you do some cleanup of accounts and write yourself a little 
script to remove home directories not owned by current users, next thing 
you know rm -r hits /home/virtfs and you remove /lib/

Wanna know a fun error message?
bash: ls: ELF Interpreter not found: /lib/

That is why directories shouldn't be hardlinked.  :)

- Kyle
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the ubuntu-users mailing list