Can I symlink multiple directories into one?
Asked Answered
B

3

39

I have a feeling that I already know the answer to this one, but I thought I'd check.

I have a number of different folders:

images_a/
images_b/
images_c/

Can I create some sort of symlink such that this new directory has the contents of all those directories? That is this new "images_all" would contain all the files in images_a, images_b and images_c?

Bedaub answered 8/1, 2009 at 5:5 Comment(0)
M
18

No. You would have to symbolically link all the individual files.

What you could do is to create a job to run periodically which basically removed all of the existing symbolic links in images_all, then re-create the links for all files from the three other directories, but it's a bit of a kludge, something like this:

rm -f images_all/*
for i in images_[abc]/* ; do; ln -s $i images_all/$(basename $i) ; done

Note that, while this job is running, it may appear to other processes that the files have temporarily disappeared.

You will also need to watch out for the case where a single file name exists in two or more of the directories.


Having come back to this question after a while, it also occurs to me that you can minimise the time during which the files are not available.

If you link them to a different directory then do relatively fast mv operations that would minimise the time. Something like:

mkdir images_new
for i in images_[abc]/* ; do
    ln -s $i images_new/$(basename $i)
done

# These next two commands are the minimal-time switchover.
mv images_all images_old
mv images_new images_all

rm -rf images_old

I haven't tested that so anyone implementing it will have to confirm the suitability or otherwise.

Mitochondrion answered 8/1, 2009 at 5:11 Comment(2)
You can use lndir to do the symlinking.Historical
just add an if statement that checks if the file already exists in the destination. no need to delete... sorry for commenting on a 11 year old postTrilby
B
11

You could try a unioning file system like unionfs!

http://www.filesystems.org/project-unionfs.html

http://aufs.sourceforge.net/

Benil answered 8/1, 2009 at 5:19 Comment(3)
And the phrase "is still in development stage" strikes fear into the hearts of even the bravest soul :-).Mitochondrion
aufs has been used in Ubuntu for a while, so it's probably stable enough.Secondly
And has by now largely been superseded by Overlayfs: en.wikipedia.org/wiki/OverlayFSBrisesoleil
K
3

to add on to paxdiablo 's great answer, i think you could use cp -s
(-s or --symbolic-link) which makes symbolic links instead of literal copying

to maybe speed up or simplify the the bulk adding of symlinks to the "merge" folder A , of the files from folder B and C.

(i have not tested this though)

I cant recall of the top of my head, but im sure there is some option for CP to NOT overwrite existing, thus only symlinks of new files will be "cp -s" ed

Karlis answered 13/2, 2019 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.