1

I'm trying to find and replace multiple directories with symlinks, but I have no idea how to do it.

I have 2 root folders, lets say /var/www/html and /var/www/html2 and they both contain the same files and folders, for example

/var/www/html/project1/lib/config
/var/www/html2/project1/lib/config
/var/www/html/project2/v1/lib/config
/var/www/html2/project2/v1/lib/config
/var/www/html/project2/v2/lib/config
/var/www/html2/project2/v2/lib/config

What I want to do is search /var/www/html and replace all config folders with a symlink to the same folder in /var/www/html2, but only if config is inside a lib folder

I understand that I will have to remove the existing directory (rm -r /var/www/html/project1/lib/config) and then create the symlink (ln -s /var/www/html2/project1/lib/config /var/www/html/project1/lib/config)

I can do this manually, but I'd like to create a 1 liner or shell script to do this.

Any help would be greatly appreciated!

2

Using bash's recursive globbing:

shopt -s globstar
cd /var/www/html
for d in **/lib/config
do
    echo rm -r "$d"
    echo ln -s /var/www/html2/"$d" "$d"
done

Run with echo to check, run without it to actually do the linking.

  • @Zanna they want to symlink to /var/www/html2/foo/lib/config from what I understand, and that's not being deleted here, unless I made a typo – muru Oct 13 '17 at 13:46
  • That does exactly what I need it to do, thank you! – nomayonnaise Oct 13 '17 at 14:17

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.