Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
Try ln -F <src> <dst> and it might work. Certainly, it used to work for the superuser in older versions of Unix. Does anyone remember whether that was UCB or System V? Yes, bad things could happen, but usually not. As I recall, rmdir knew not to carry on deleting past a hard link. However, users could get confused and delete things in error. – Steve Pitchers Dec 10 '14 at 22:53
@StevePitchers How can rmdir handle hard links in a special way? A hard link is just a normal link - but an additional one. It is not even easy to find out whether an unusual extra links exist without extra recordings. – Volker Siegel Jan 15 '15 at 10:33
Each node stores the number of hard links that point to it: the contents are only released once there are no remaining links. So rmdir can tell whether the directory has links from other places. Recursive removal, rm -r, must be coded with care, to be sure it will act correctly even should there be errors like "permission denied". BTW, UCB = BSD, doh! – Steve Pitchers Jan 15 '15 at 19:01
I have done ln -F on directories and have it work. But you don't dare delete the directory afterwards for fear of corrupting the file system. – Edward Falk Mar 10 at 19:37
up vote 46 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
regarding the last paragraph, if you edit "copied" hardlinked file, the original file is also changed - see… – marcin Jun 20 '14 at 10:55
This description of hard links is rather misleading. It's basically true that hard links "keep the file that they reference from being deleted", but that's just a side effect of hard links. It's certainly NOT true that you can create hard links in one directory, change the "original" file, and then expect the hard links to somehow point to the old content. In fact, the guiding truth of hard links is the fact that it's not a link at all, at least not any more so than the original "file", which is just a name pointing to a file. A hard link is simply another name pointing to the same file. – matty Jul 9 '15 at 2:32
The backup idea is good and I actually use that a lot, but I think users should be warned that changing a file will also change the backup. – Mark Aug 18 '15 at 9:10
Heck, a symlink need not point to anything at all. ln -s "Don't use this directory" README is legitimate. In fact, if you think about it, a directory can be used as a relational database and not contain any actual files at all. – Edward Falk Mar 10 at 19:39

Directory hardlinks break the filesystem in multiple ways

They allow you 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, in a consistent way. Similar for the equally important locate 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, multiple 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


are different files.
There are infinitely many further paths of the file. They are the same in terms of their 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 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

How can soft links to directories work then?

A path that may contain softlinks and even soft linked directory loops is often used just to identify and open a file. It can be used as a normal, linear path.

But there are other situations, when paths are used to compare files. In this case, symbolic links in the path can be resolved first, converting it to a minimal, and commonly agreed upon representation creating a canonical path:

This is possible, because the soft links can all be expanded to paths without the link. After doing that with all soft links in a path, the remaining path is part of a tree, where a path is always unambiguous.

The command readlink can resolve a path to its canonical path:

$ readlink -f /some/symlinked/path

Soft links are different from what the filesystem uses

A soft link cannot cause all the trouble because it is different from the links inside the filesystem. It can be distinguished from hard links, and resolved to a path without symlinks if needed.
In some sense, adding symlinks does not alter the basic file system structure - it keeps it, but adds more structure like an application layer.

From man readlink:

        readlink - print resolved symbolic links or canonical
        file names

        readlink [OPTION]... FILE...

        Print value of a symbolic link or canonical file name

        -f, --canonicalize
               canonicalize by  following  every  symlink  in
               every component of the given name recursively;
               all but the last component must exist
        [  ...  ]
share|improve this answer
+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 '14 at 11:21
Why can't a soft link do all this? – Tanay May 26 '15 at 5:57
@Tanay Right, it could help the expanation to compare it to similar cases with soft links. Ill try. – Volker Siegel May 26 '15 at 17:23

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
I've used mount --bind <src> <dest>. Use with care not to wipe the src ;) – kachar Feb 17 '14 at 8:46
I get: mount: unknown filesystem type 'bind' – Wizek Feb 25 at 16:04

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
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 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 '14 at 1:34
the link " they break the file-system structure" does not work. – Charlie Parker Jun 15 '14 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 '14 at 11:32

Your Answer


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.