Create symbolic links with wildcards


Solution 1

You might want to do that:

for dir in dir1 dir2
  [[ ! -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 {} \;

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 {} \;

Solution 2

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:


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.


Related videos on Youtube

Author by


Updated on June 30, 2022


  • Haukur
    Haukur less than a minute

    I have multiple hard drives with the same directory hierarchy, for example:


    Two hard drives with similar names and similar directory names. I want to create separate symbolic links to dir1 and dir2 on every hard drive. The easiest way I have found is to use cp -sR:

    cp -sR /media/sd*/dir1 /somedir/dir1
    cp -sR /media/sd*/dir2 /somedir/dir2

    However, this creates new directories in /somedir which has various side effects, for example, the directory timestamps are useless.

    How can I create symbolic links named dir1 and dir2 which link to /media/sd*/dir1 and /media/sd*/dir2? Files are regularly added to the hard drives so I would need to run these commands on a regular basis.

    • janos
      janos almost 10 years
      When you say "symbolic link named dir1 which link to /media/sd*/dir1" that sounds like a link pointing to multiple places which is not possible: a link can only point from one place to one place. Maybe you want all the files under /media/sda/dir1/* and /media/sdb/dir1/* to be linked to from /somedir/dir1/* ? It would seem to me that unionfs over the two disks would better serve your purpose then symlinks.
  • Haukur
    Haukur almost 10 years
    I tried this but it didn't work as expected. It essentially copied the hierarchy of /media: it resulted in /somedir/media/a/..., /somedir/media/b/..., etc instead of creating /somedir/data as one folder.
  • jlliagre
    jlliagre almost 10 years
    Sorry, there was a bug in my script. Answer edited.
  • Haukur
    Haukur almost 10 years
    Thanks but this is not what I wanted to do. Sorry if I wasn't clear enough.
  • Trevor
    Trevor almost 2 years
    It was what I wanted to do though. Thanks!