10

I would like to have Ubuntu's default music folder /home/$USER/Music to act pretty much as a library would in Windows. Essentially it would be a virtual folder containing the contents of other folders.

My rationale is Rhythm Box will only watch one folder for new music. All my music resides in two folders /home/$USER/Dropbox/Music and /home/$USER/Dropbox/New Music.

This also prove useful to show the contents of other Dropbox folders in Ubuntu's default folders (Documents, Pictures, etc).

I did search and tried to find the answer on my own to no avail. I'm sure there is a way to accomplish this; I'm just missing it.

Thanks in advance.

11
  • 1
    There is the ability to bind 2 folders together with: sudo mount --bind olddir newdir but this is probably not exactly what you are after...
    – andrew.46
    May 3, 2016 at 7:22
  • @andrew.46 mount --bind allows access to one set of folder from two different mount points. OP wants to access two sets of folders from one mount point
    – EdiD
    May 3, 2016 at 7:40
  • @Edid: Oops, you are of course perfectly correct... sorry for the noise...
    – andrew.46
    May 3, 2016 at 7:49
  • I could think of a way, but it would require a small background script. Would that be acceptable? May 3, 2016 at 7:53
  • @JacobVlijm Please don't tell me the script will check if any file is available on the directory and copy to two destinations afterwards..
    – heemayl
    May 3, 2016 at 7:57

2 Answers 2

8

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
4
  • Jacob, I really have to thank you for going all out and writing a script for me. Now I have to use it, you did the work. :-). I'll give this a shot over the weekend. Many thanks. May 26, 2016 at 21:02
  • 1
    @DanHutter Thanks! Hope you'll like it. If there is anything, please let me know. May 26, 2016 at 21:04
  • Hi Jacob, I just upgraded to Ubuntu Gnome 16.10 this script still works perfectly. Currently using it for Music, Photos, and Documents! Thank you again. Dec 17, 2016 at 8:22
  • @DanHutter Thanks for mentioning, that is so good to hear! Dec 17, 2016 at 11:14
3

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

You must log in to answer this question.

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