If I have 2 directories, a and b can anyone suggest a convenient method of creating a directory that appears to have the contents of both a and b without copying the files ? At the moment what I have been using is ln -s * to make a load of sym links and then delete the sym links when I am done. I would prefer it though if the contents of sub directories appeared to be merged, and managing this with the sym link method may need some fiddling.

It's OK if the resultant directory is read only given the application.

| improve this question | | | | |
  • Can't you just symlink the directory itself instead of all its contents? I.e. ln -s a/ b would create a symlinked directory b that points to a. – Daniel Andersson Feb 28 '13 at 13:00
  • 1
    @Daniel Andersson That would not create a directory appearing to show the merged contents of a and b which was the point of the question. – camelccc Feb 28 '13 at 13:03
  • It is kind of non-trivial. How do you merge collisions for files contained in both a and b? What is your intention? You can try to hard-link files from a and b to another directory c, so you get files update on change in both directories. – p4553d Feb 28 '13 at 13:40
  • I know it's non trivial, I'd like collisions to make whichever file is newest appear in the merged directory. I can't see how hard linking helps at all over soft linking though, and it won't work if a and b are on different drives. – camelccc Feb 28 '13 at 13:53
  • Which OS/distro are you using? – Dennis Feb 28 '13 at 16:55

You can effectively merge to directories with UnionFS by overlaying the two directories (called branches in this context) into one single mount point.

There are several implementations of UnionFS; UnionFsFuse is one of the simpler ones, since it is an application (rather than a kernel patch).

The website contains a link to their archive. Compiling the source code should be straightforward on most distros.

In Ubuntu, e.g., it suffices to install the package unionfs-fuse by executing the following command:

sudo apt-get install unionfs-fuse

Once installed, you can proceed to overlay the directories. There are a variety of options. I'll briefly explain the most basic ones:

  • The command

    unionfs-fuse foo:bar baz

    overlays the contents of the directories foo and bar in the directory baz.1

    This overlay will be read-only; any attempt to create or modify a file in baz will result in a Permission denied. error. However, you'll still be able to create and modify the files inside foo and bar themselves.

    Collisions are handled in a fixed way: If a file exists in a higher branch (foo), it will have preference over a file of the same name in a lower branch (bar).

  • The command

    union-fuse foo=RW:bar=RO baz

    makes foo writable and bar (read-only). Modifying files of foo will be possible, but modifying files of bar won't be. All newly created files in baz will appear in foo.

  • The command

    union-fuse -ocow foo=RW:bar=RO baz

    is similar to the last command. The copy on write option (cow) has the effect that attempting to modify a file of bar inside baz will result in a modified copy of the file in foo (or any other higher-level writable branch).

  • With the command

    union-fuse foo=RW:bar=RW baz

    all can be modified. New files will be created in the top branch (foo).

  • The command

    union-fuse foo=RO:bar=RW baz

    allows modifying the files of bar inside baz, but no new files can be created.

  • The command

    sudo umount baz

    undoes the union mount.

See also: man unionfs-fuse

1 baz must exist before this command is executed. This applies to all further commands as well.

| improve this answer | | | | |

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.