Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I symlink multiple directories into one?

Tags:

unix

symlink

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?

like image 683
nickf Avatar asked Jan 08 '09 05:01

nickf


People also ask

Can symbolic links link directories?

Symbolic links allow you to access specific files or directories from your current location, which is similar to how we use desktop shortcuts.

Can you chain symlinks?

Yes. You can only stack so much symbolic links together before the kernel and/or application refuse to follow the chain.

How do I create a soft link between two folders?

Use the -s option to create a soft (symbolic) link. The -f option will force the command to overwrite a file that already exists. Source is the file or directory being linked to. Destination is the location to save the link – if this is left blank, the symlink is stored in the current working directory.

Do symlinks work both ways?

Yes, a symbolic link is a pointer to another location. This means that any changes you make are in fact updating at the target location.


2 Answers

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.

like image 58
paxdiablo Avatar answered Sep 30 '22 03:09

paxdiablo


You could try a unioning file system like unionfs!

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

http://aufs.sourceforge.net/

like image 37
chickeninabiscuit Avatar answered Sep 30 '22 02:09

chickeninabiscuit