1

I'm well aware of the difference between symbolic and hard links. I'm also aware of the dangers of creating hard links to directories.

Nevertheless, I'm insisting on creating a hard link to a directory and all of its subdirectories (on the same partition).

However, the following does not work:

root@fab-ux:/home/fab-user/Public
# ln --directory ../Documents/CV/ CV
ln: failed to create hard link ‘CV’ => ‘../Documents/CV/’: Operation not permitted

Is there a kernel/File System (Running on EXT4) parameter that will allow me to accomplish this foolishness?

Yes, I can mount bind, can hard link individual files through a cron job, ... But the point is that I want both the convenience of physically only one directory and all its files and the convenience of only uploading certain hard-linked directories in my Public directory to the cloud and the cloud software detects symbolic links and doesn't support them and most important of all: I'm running Ubuntu! Not OSX nor Windows: I want the system to do what I want!

0

2 Answers 2

4
+100

As long as you stay on a current kernel (it is a feature by the kernel rather then one of the filesystem) there is nearly no way around this limitations.

Limitations of hard links

To prevent loops in the filesystem, and to keep the interpretation of .. (parent directory) consistent, many modern operating systems do not allow hard links to directories. UNIX System V allowed them, but only the superuser had permission to make such links.

The Open Group Base Specifications Issue 7 (excerpt)

...Linking to a directory is restricted to the superuser in most historical implementations because this capability may produce loops in the file hierarchy or otherwise corrupt the file system. This volume of POSIX.1-2008 continues that philosophy by prohibiting link() and unlink() from doing this. Other functions could do it if the implementor designed such an extension...


Because there is no longer a system call like link() which don't checks the oldpath being a regular file the only way out may be creating a similar function to linkat() doing it your desired way.

2
  • Upvoted already, but waiting an additional 48 hours before accepting, just in case someone else has done this before! ;-)
    – Fabby
    Apr 4, 2016 at 23:44
  • Crap! Forgot to accept! (done)... Remind me in 2 weeks to add the bounty I promised so long ago! ;-) (no access to AU all next week)
    – Fabby
    May 1, 2016 at 13:23
1

You can not create hard links to directories in linux. I have heard that OSX allows this for time machine to use, but not linux. The closest you can do is cp -l, which will create a new tree of directories mirroring the original, but hard link all of the regular files.

4
  • Yeah, mount --bind is what I ended up doing... :/
    – Fabby
    May 5, 2016 at 16:09
  • 1
    wrong. you can, its just not easy. this works for me: dd if=/dev/zero of=/ext4.disk bs=10M count=1; mkfs.ext4 /ext4.disk; mkdir /m; mount /ext4.disk /m; mkdir /m/a /m/b; umount /m;sync; echo 3 > /proc/sys/vm/drop_caches; echo 'link /a /b' | debugfs -w /ext4.disk | sed '$s/debugfs: $//' | sed '1d';mount /ext4.disk /m; - now /m/b is a hardlink to /m/a
    – hanshenrik
    Dec 23, 2017 at 4:00
  • @hanshenrik, now run a fsck on the volume and see if it complains that it is invalid and wants to delete one of the links. You can do all sorts of crazy things with debugfs or otherwise directly tampering with the filesystem.
    – psusi
    Dec 24, 2017 at 1:17
  • @psusi you're right, fsck was not happy about it. root@x-ratma-net:/# fsck.ext4 -f ext4.disk e2fsck 1.43.4 (31-Jan-2017) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Entry 'a' in /b (1281) is a link to directory /a (12). Clear<y>? yes Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information ext4.disk: ***** FILE SYSTEM WAS MODIFIED ***** ext4.disk: 14/2560 files (0.0% non-contiguous), 1528/10240 blocks root@x-ratma-net:/#
    – hanshenrik
    Dec 24, 2017 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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