5

First of all, I know that you can't create hardlinks of a folder.

I was wondering if there was any command (linux) that could automatically (and recursively) create the subsequent folder-tree at the destination and then hardlink all the files from source to destination automatically.

2 Answers 2

6

I'm not sure what you mean by "then hardlink all the files from source to destination automatically". And you explicitly want recursion.

If you want the two locations to stay "synced", so e.g. removing or creating a file should be observed in both of them at once, then bind mounting (this other answer) is good. Note the solution leaves no trace in the filesystem. If you create a hardlink, it's created in the filesystem. Move the disk and mount the filesystem in another OS and the hardlink is still there. Reboot and the hardlink is still there. Bind mounts are on the OS level. Not only you would need to separately command the other OS to bind mount after you move and mount the disk. If you don't move the disk, you still need to tell the current OS to bind mount again after it reboots (if permanent solution is what you want).

A symlink to a directory is somewhat similar and it exists in the filesystem.

ln -s /original /other

If the symlink and its target belong to the same filesystem and the symlink is relative, then it will work even if the filesystem gets mounted elsewhere (different mountpoint, possibly in different OS). There are reasons a symlink may not be what you want (example).

Note bind mounting or symlinking is not really recursive. It's a single act that "links" two paths. It does nothing to deeper paths. Their (semi-)equivalence arises because there's some kind of link between respective ((((…)great-)great-)grand)parent "directories".

Now if by "then hardlink all the files from source to destination automatically" you mean "do this automatically but once", then you can do this with cp:

cp -lR /original /other

(-l is not required by POSIX though). It's like cp -R but with hardlinks. This is really recursive. But it's also one-time action. The directory subtrees are separate, only the regular files come up hardlinked. File creation or deletion will not be mirrored. Actions that break hardlinks will break hardlinks.

1
  • Amazing. Exactly what I needed. Thank you!
    – RazorHail
    Commented Feb 7, 2020 at 16:34
7

Would a bind mount be a better solution? It does exactly what you want, which is to have all the files be the same recursively at two different paths.

This command makes /other have the same contents as existing mount or directory /original:

mount --bind /original /other

Any changes you make in either /original or /other appear immediately in the other because the two paths are now pointers to the same mount.

7
  • 2
    Thanks, but I need the hardlinks to work both on the host-system and my docker-container. the contents of the mount--bind folders don't show up inside the container
    – RazorHail
    Commented Feb 6, 2020 at 14:32
  • 1
    @RazorHail You should have stated this in the question. Now it's (IMO) too late because you shouldn't invalidate existing answers by adding extra conditions. The existing answers genuinely try to solve what appeared to be your problem (judging by the original form of the question). If no solution works with your container, then maybe ask another question. This time you should ask about your real problem (compare this). You may link to this question to indicate what you have already tried; and explain why these solutions are not good for you. Commented Feb 6, 2020 at 15:12
  • @RazorHail Does this help? How to mount a host directory in a Docker container? Commented Feb 6, 2020 at 15:17
  • 2
    @RazorHail: It sounds like you are not configuring your Docker container mounts correctly. Docker bind mounts put a host mount into the container. If your question is about Docker, you should have stated it in the question and provided details on how you set up or want to set up your container's storage.
    – Deltik
    Commented Feb 6, 2020 at 15:31
  • 1
    @RazorHail That works for me. Created a directory, did a mount --bind on another directory, started a container with bash, with a -v to bind-mount a common parent of the source and the bound directories in the container, and I can create files in the bound directory, and they are visible on the host in both directories. So I suggest to ask a different question, and describe what you tried.
    – xenoid
    Commented Feb 6, 2020 at 17:08

You must log in to answer this question.

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