2

Ok, so I've been trying to get my head around this, but I'm struggling.

The premise is this: I have a directory with lots of subdirectories (some of which also contain more subdirectories), and I've got another separate directory on a different share which mimics the source directory in the layout. What I need now is a way of looping through the source directory, discovering the files in the subdirs, and then creating symlinks to them in the destination dir.

In case this isn't that clear, this post describes it fairly well, except that that question is aimed at symlinking dirs, rather than the files themselves.

edit: just noticed what Kerrek was getting at, forgot to include this link: Bash script to automatically create symlinks to subdirectories in a tree

Ok, so so far I have this, based off of Kerrek's answer:

#!/bin/bash

SOURCE="/home/simon/testdir/src"
DEST="/home/simon/testdir/dest"

cd $DEST

find $SOURCE -type f -exec ln -s -- "{}" "{}" \;

exit

which gives the following:

ln: creating symbolic link `/home/simon/testdir/src/new.dir/a': File exists
ln: creating symbolic link `/home/simon/testdir/src/new.dir/b': File exists
ln: creating symbolic link `/home/simon/testdir/src/new.dir/c': File exists

however, it doesn't actually create the symlinks in the destination dir.

Community
  • 1
  • 1
analbeard
  • 45
  • 3
  • 13
  • Did you just make a reference to your own very same post, claiming that it describes your posts point very well? – Kerrek SB Mar 20 '12 at 21:53
  • @KerrekSB yes, but I'm unsure as to how to adapt it to use in my situation. I've never really worked with symlinks before. – analbeard Mar 20 '12 at 21:55
  • You need to say `ln -s {} ./{} \;` if you're already in the destination directory. Or just do what I wrote in the answer. – Kerrek SB Mar 20 '12 at 22:27
  • see, i tried that and now i get: ln: creating symbolic link `.//home/simon/testdir/src/new.dir/c': No such file or directory edit: i wonder if the ln command is receiving the entire path of the source directory, and then trying to apply it to the symlink it's creating in the destination directory? – analbeard Mar 20 '12 at 22:28
  • `ln -sf` will *replace* existing files, but you need to get your script right first. – 0xC0000022L Mar 20 '12 at 22:31
  • Sorry, you have to `cd` into the *source* directory and omit the directory from `find` so that you get the relative path names. I'll edit my answer. – Kerrek SB Mar 20 '12 at 22:32
  • Cheers @KerrekSB, I picked up on that as you were typing your reply, I think. – analbeard Mar 20 '12 at 22:33

1 Answers1

5

How about using find?

cd -- "$SOURCEDIR"

find -type d -exec mkdir --parents -- "$DESTDIR"/{} \;

find -type f -exec ln --symbolic -- "$SOURCEDIR"/{} "$DESTDIR"/{} \;
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084