Make Folder a symbolic link to two folders?

Solution 1:

Scripted solution(s)

1. Automatically updated library view

With the background script below, you can set up a directory, virtually showing the combined content of an arbitrary number of other folders.

The content of the folder (existing of symlinks) is dynamically synchronized with the (combined) sources.

How it works

The script periodically lists the content of the source folders as well as the targeted folder for the "library" -view.

  • If there are items in any of the source folders, not existing in the library, a symlink will be created in the library.
  • if there are items in the library, not existing in any of the source folders (actually a broken link), the item is removed from the library.

enter image description here

Comparing lists is extremely light weight (programmatically) and low on resources. I tested the script with a much faster loop then in the script below, with a number of items of about 500 in a single directory level, with no additional (noticable) load whatsoever.

The script and how to use

#!/usr/bin/env python3
import os
import sys
import time

# --- set loop time below (or leave it as it is)
loop = 5
# ---
# don't change anything below

target = sys.argv[1]
sources = sys.argv[2:]

while True:
    currlinks = os.listdir(target)
    compare = []
    for dr in sources:
        for f in os.listdir(dr):
            compare.append(f)
            if not f in currlinks:
                # create link
                os.symlink(dr+"/"+f, target+"/"+f)
    # clean up possible broken links
    for link in currlinks:
        if not link in compare:
            os.remove(target+"/"+link)
    # loop time
    time.sleep(loop)
  1. Copy the script into an empty file, save it as library_view.py
  2. Test- run the script with the targeted directory, to view the combined directories in, as first argument, the source directories as next arguments, e.g.:

    python3 /path/to/library_view.py /path/to/virtual_library /path/to/source1 /path/to/source2
    

    This command will show the content of the folders source1 and source2 in virtual_library.

  3. If all works fine, add it to startup applications: Dash > Startup Applications > Add. Add the command:

    python3 /path/to/library_view.py /path/to/virtual_library /path/to/source1 /path/to/source2
    

Note

As mentioned, you can add more than two soure directories if you like.

2. Manual version

If you would prefer not to use a background script for some reason, you can manually update (synchronize) the library folder, with exactly the same command under a shortcut key, using the script below:

#!/usr/bin/env python3
import os
import sys

target = sys.argv[1]
sources = sys.argv[2:]

currlinks = os.listdir(target)
compare = []
for dr in sources:
    for f in os.listdir(dr):
        compare.append(f)
        if not f in currlinks:
            # create link
            os.symlink(dr+"/"+f, target+"/"+f)
# clean up possible broken links
for link in currlinks:
    if not link in compare:
        os.remove(target+"/"+link)

Choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command yo a key combination of your choice:

python3 /path/to/library_view.py /path/to/virtual_library /path/to/source1 /path/to/source2

Solution 2:

You can install unionfs-fuse for this:

sudo apt-get install unionfs-fuse 

Mounting:

unionfs-fuse /folder1=RW:/folder2=RW /mount/point

It will group all content from folder1 and folder2 (or more if you want) to your mount point

Umounting:

sudo umount /mount/point