I am using Ubuntu Linux and, just for fun, I want to create a hardlink to a directory (as seen here). Because I'm just doing this for fun, I'm not looking for any sort of pre-developed directory-hardlinking software that someone else wrote, I want to know how to do it myself. So, how do I directly, manually, modify an inode?

Ideally I would like the answer as a Linux command that I can run from the Bash command line, but if there is no way to do it from there I would also accept information on how to do it in C or (as a last resort) assembly.

  • 1
    Want to break the filesystem? There certainly are simpler ways... – vonbrand Mar 26 '13 at 15:54

That depends on the filesystem. For ext4, you can do this with debugfs as follows:

dennis@lightning:/tmp$ dd if=/dev/zero of=ext4.img bs=1M count=100
104857600 bytes (105 MB) copied, 0.645009 s, 163 MB/s
dennis@lightning:/tmp$ mkfs.ext4 ext4.img 
mke2fs 1.42.5 (29-Jul-2012)
ext4.img is not a block special device.
Proceed anyway? (y,n) y
Writing superblocks and filesystem accounting information: done 
dennis@lightning:/tmp$ mkdir ext4
dennis@lightning:/tmp$ sudo mount ext4.img ext4
dennis@lightning:/tmp$ mkdir -p ext4/test/sub/
dennis@lightning:/tmp$ sudo umount ext4
dennis@lightning:/tmp$ debugfs -w ext4.img 
debugfs 1.42.5 (29-Jul-2012)
debugfs:  link test test/sub/loop
dennis@lightning:/tmp$ ls ext4/test/sub/loop/sub/loop/sub/loop/sub/loop/sub/loop/
total 1
drwxrwxr-x 2 dennis dennis 1024 mrt 26 12:15 sub


  • you cannot link directly to the parent, so foo/bar can't be a link to foo, hence the extra directory.
  • You should not run debugfs on mounted filesystems. If you do, you will need to unmount/mount after making changes.

Tools like find and ls still won't loop:

dennis@lightning:/tmp$ find ext4
find: `ext4/lost+found': Permission denied
find: File system loop detected; `ext4/test/sub/loop' is part of the same file system loop as `ext4/test'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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