0

I wrote a symlink script for someone on another team to run in a directory I don't have access to, and it seemed to have caused something unexpected.

destination_path=/path/to/dest/ source_path=/path/to/src for dir in first second do # replace existing symlink for subdir in {1000..1020} do ln -sf $source_path/$dir/$subdir $destination_path/$dir/$subdir done done 

Before the script was run path/to/dest/$dir contains symlinks 1000, 1001, ..., 1020 that's linked to a different set of directories. My script is intended to replace those symlinks with symlinks to /path/to/src/$dir/$subdir, but instead of doing that, it seems to have created the symlinks within the existing symlink.

So for example, /path/to/dest/$dir/1000 used to be a symlink, and after running my script /path/to/dest/$dir/1000 is still points to the same place but within /path/to/dest/$dir/1000, there is a sym link 1000 that points to $source_path/$dir/$subdir/1000.

This shouldn't have occurred with my script right? I tried to reproduce this locally with a mocked destination directory structure and I couldn't reproduce this issue.

Edit: upon further research, it seems I should have used ln -sfn instead of just ln -sf? What is the easiest way to remedy this?

1 Answer 1

1

A symlink that points to a directory is (almost always) treated as that directory. In general if you want to update a symlink, remove it and recreate it.

Use a variation of your script like this,

#!/bin/bash destination_path='/path/to/dest/' source_path='/path/to/src' for dir in first second do # replace existing symlink for subdir in {1000..1020} do s="$source_path/$dir/$subdir" t="$destination_path/$dir/$subdir" [ -d "$t" ] && [ -h "$d/$subdir" ] && echo rm -f -- "$d/$subdir" [ -h "$d" ] && echo rm -f -- "$d" echo ln -sf -- "$s" "$d" done done 

Remember to remove all three instances of echo when you're happy it's going to do what you need it to do.

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.