Create symbolic links with wildcards

This will create symlinks /somedir/dir1/* pointing to /media/sd*/dir1/*.

mkdir /somedir/dir1
ln -sf /media/sd*/dir1 /somedir/dir1

If a file or directory exists in more than one /media/sd*/dir1/ then the link will point to the last one, for example if you have:

/media/sda1/dir1/Movies
/media/sda1/dir1/Pictures
/media/sdb1/dir1/Movies
/media/sdb1/dir1/Data

you will get:

/somedir/dir1/Movies -> /media/sdb1/dir1/Movies
/somedir/dir1/Pictures -> /media/sda1/dir1/Pictures
/somedir/dir1/Data -> /media/sdb1/dir1/Data

Not sure if this is what you want though.

It seems to me that unionfs mounting over /media/sd* would better serve your purpose than symlinks.


You might want to do that:

for dir in dir1 dir2
do
  [[ ! -d /somedir/$dir ]] && mkdir /somedir/$dir    
  find /media/sd*/$dir -type f -exec bash -c \
    '[[ ! -f /somedir/'$dir'/$(basename $1) ]] && ln -s $1 /somedir/'$dir'/' foo {} \;
done

This create symbolic links in /somedir/dir1/ (resp. dir2) pointing to all files present under /media/sd*/dir1 (resp. dir2). This script doesn't preserve hierarchy that might be present under the source directories.

Edit: Should you want all the links to be placed in a single directory, here is a slightly modified version:

[[ ! -d /somedir/data ]] && mkdir /somedir/data    
find /media/sd*/dir[12] -type f -exec bash -c \
    '[[ ! -f /somedir/data/$(basename $1) ]] && ln -s $1 /somedir/data/' foo {} \;
done

Tags:

Symlink

Ln

Cp