Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am using Ubuntu 12.04 and I am pretty new Linux world. I am really amazed when I am trying to create hard link for any directory but It failed. I can create hard links for files inside filesystem boundry. I know reason why we can not create hardlinks for files beyond file system.

I tried this command

nischay@NischayLaptop:~$ ln /Some/Direcoty /home/nischay/Hard-Directory
hard link not allowed for directory
nischay@NischayLaptop:~$ sudo ln /Some/Direcoty /home/nischay/Hard-Directory
[sudo] password for nischay: 
hard link not allowed for directory

I just want to know the reason behind this. Is it same for all GNU/Linux distos and Unix flavours (BSD,Solaris,HP-UX,IBM Aix) or only in Ubuntu or Linux.

share|improve this question
3  

3 Answers 3

up vote 26 down vote accepted

"You generally should not use hard links anyway" is over-broad. You need to understand the difference between hard links and symlinks, and use each as appropriate. Each comes with its own set of advantages and disadvantages:

Symlinks can:

  • Point to directories
  • Point to non-existent objects
  • Point to files and directories outside the same filesystem

Hard links can:

  • Keep the file that they reference from being deleted

Hard links are especially useful in performing "copy on write" applications. They allow you to keep a backup copy of a directory structure, while only using space for the files that change between two versions.

The command cp -al is especially useful in this regard. It makes a complete copy of a directory structure, where all the files are represented by hard links to the original files. You can then proceed to update files in the structure, and only the files that you update will take up additional space. This is especially useful when maintaining multigenerational backups.

share|improve this answer
1  
regarding the last paragraph, if you edit "copied" hardlinked file, the original file is also changed - see unix.stackexchange.com/questions/70531/… –  marcin Jun 20 at 10:55

The reason hard-linking directories is not allowed is a little technical. Essentially, they break the file-system structure. You should generally not use hard links anyway. Symbolic links allow most of the same functionality without causing problems (e.g ln -s target link).

share|improve this answer
4  
Hard links have good use cases. Saying you should generally not use them is a little too broad. –  Sander Steffann Nov 28 '13 at 8:34
2  
+2 for providing the link that actually answers the OP's question (and mine), -1 for emitting an opinion ("You should generally not use hard links anyway" - if it had links to support it, it would be ok). Which was good, because I can't give +2 anyway. ;D –  msb Jan 14 at 1:34
1  
the link " they break the file-system structure" does not work. –  Charlie Parker Jun 15 at 4:01

FYI, you can achieve the same thing as hard links for directories by using mount:

mount -t bind /var/www /home/user/workspace/www

This is very dangerous because most tools and programs will not be aware of the binding. I once did something like in the above example and then proceeded to rm -rf /home/user. Luckily, there was nothing relevant in /var/www.

share|improve this answer
3  
I've used mount --bind <src> <dest>. Use with care not to wipe the src ;) –  kachar Feb 17 at 8:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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