4

Before I get 100s of answers that tell me it is impossible to hardlink directories in linux: yes, I know that.

The file in question appeared in lost+fount after I checked the filesystem with e2fsck (its an ext4) and according to stat it /is/ a file with 2 hardlinks:

# stat --format="File \"%n\" is a %F with %h hardlinks" ./#30934353
File "./#30934353" is a directory with 2 hardlinks

Doh. How can I safely delete them without touching the included files? I tried rm and unlink, both told me that die directory is not empty…

2
  • A directory with a link count of 2 is a perfectly normal and healthy directory (e.g. here). Do you really have a problem?
    – Mat
    Commented May 14, 2013 at 6:44
  • You are right, thank you! I completely misunderstood the output of e2fsck and stat. I just "rm -R"ed the directories and all is fine :)
    – Markus
    Commented May 14, 2013 at 7:43

1 Answer 1

4

Directories on ext4 file systems generally have at least 2 links. The entry in their parent directory and the . entry in themselves. See there for more details.

Having said that, if the file system is inconsistent, you may very well have a directory linked in more than one place and the link count not to reflect it.

For instance, with debugfs -w (on an unmounted fs), you can do link a/b c/d to force the creation of a link to a directory. You'll notice that the link count is not updated (you'd need sif a/b links_count 3 to update it):

$ ls -lid a/b c/d a/b/. c/d/.
12 drwxr-xr-x 2 chazelas chazelas 1024 May 14 08:37 a/b/
12 drwxr-xr-x 2 chazelas chazelas 1024 May 14 08:37 a/b/./
12 drwxr-xr-x 2 chazelas chazelas 1024 May 14 08:37 c/d/
12 drwxr-xr-x 2 chazelas chazelas 1024 May 14 08:37 c/d/./

The link count is 2 above, even though there are 3 links (to a, to c, and the "." to itself).

Note that fsck would report a problem with that:

Pass 2: Checking directory structure
Entry 'd' in /c (1282) is a link to directory /a/b (12).
Clear<y>?

So it's unlikely to be your case here if the fs has just gone through a fsck.

To remove one of those links, you can use debugfs -w again (after having unmounted the filesystem) and do unlink c/d in it, or use fsck. If a directory has more than one link, you can check the .. entry within it (with debugfs ls) to check which is the correct one (.. will have the same inode number as its correct parent).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .