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 try to create hard link for any directory and fail. I can create hard links for files inside file system boundary. I know the reason why we cannot create hardlinks for files beyond file system.

I tried these commands:

$ ln /Some/Direcoty /home/nischay/Hard-Directory
hard link not allowed for directory
$ 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 distros and Unix flavours (BSD, Solaris, HP-UX, IBM AIX) or only in Ubuntu or Linux.

share|improve this question
5  

4 Answers 4

up vote 32 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
4  
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
5  
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
4  
+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
    
Try to summarize the contents of the link in the answer, and keep the link as a reference. This a Stack Exchange good practice to avoid link rot, thanks. –  Oxwivi Sep 17 at 11:32

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

Directory hardlinks break the filesystem in multiple ways

They allow to create loops

A hard link to a directory can link to a parent of itself, which creates a file system loop. For example, these commands could create a loop with the back link l:

mkdir -p /tmp/a/b
cd /tmp/a/b
ln -d /tmp/a l

A filesystem with a directory loop has infinite depth:

cd /tmp/a/b/l/b/l/b/l/b/l/b

Any find command without the -maxdepth predicate will run into an infinite loop. That means you can no longer use find, which is an important command

A tree, by definition, has no loops, so the file system is no longer a tree.

They break the unambiguity of parent directories

With a filesystem loop, pultiple parent directories exist:

cd /tmp/a/b
cd /tmp/a/b/l/b

In the first case, /tmp/a is the parent directory of /tmp/a/b.
In the second case, /tmp/a/b/l is the parent directory of /tmp/a/b/l/b, which is the same as /tmp/a/b.
So it has two parent directories.

They multiply files

Files are identified by paths, after resolving symlinks. So

/tmp/a/b/foo.txt
/tmp/a/b/l/b/foo.txt

are different files.
There are infinitely many further paths of the file. They are the same in terms of inode number of course. But if you do not explicitly expect loops, there is no reason to check for that.

A directory hardlink can also point to a child directory, or a directory that is neither child or nor parent of any depth. In this case, a file that is a child of the link would be replicated to two files, identified by two paths.

Your example

$ ln /Some/Direcoty /home/nischay/Hard-Directory
$ echo foo > /home/nischay/Hard-Directory/foobar.txt
$ diff -s /Some/Direcoty/foobar.txt /home/nischay/Hard-Directory/foobar.txt
$ echo bar >> /Some/Direcoty/foobar.txt
$ diff -s /Some/Direcoty/foobar.txt /home/nischay/Hard-Directory/foobar.txt
$ cat /Some/Direcoty/foobar.txt
foo
bar
share|improve this answer
3  
+1 Best answer IMO. The accepted answer speaks about advantages/disadvantages of symlinks vs. hardlinks, but doesn't actually answer the original question. I was astonished that nobody actually mentioned the rationale behind the fact you can't create hardlinks to directories, until I finally came to this post ;) –  Malte Skoruppa Sep 17 at 11:21

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.