Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

/media/sda/dir1
/media/sda/dir2
...
/media/sdb/dir1
/media/sdb/dir2

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.

share|improve this question
2  
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 '12 at 4:50

2 Answers 2

up vote 1 down vote accepted

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
share|improve this answer
    
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 '12 at 22:14
    
Sorry, there was a bug in my script. Answer edited. –  jlliagre Aug 22 '12 at 23:04

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/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.

share|improve this answer
    
Thanks but this is not what I wanted to do. Sorry if I wasn't clear enough. –  Haukur Aug 22 '12 at 23:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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