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.

  • 3
    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.
    – janos
    Aug 22, 2012 at 4:50

2 Answers 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.

  • Thanks but this is not what I wanted to do. Sorry if I wasn't clear enough.
    – Haukur
    Aug 22, 2012 at 23:39
  • It was what I wanted to do though. Thanks!
    – Trevor
    Aug 18, 2020 at 3:33

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 {} \;
  • 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.
    – Haukur
    Aug 22, 2012 at 22:14
  • Sorry, there was a bug in my script. Answer edited.
    – jlliagre
    Aug 22, 2012 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.