Linux - Fix multiple broken symlinks
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I have a list of multiple files in which the symbolic link is broken. Instead of doing it one by one how can I re-link all of them by reading from the example list below?
The closest answer I was able to find was this ( solution.

Ideally, if I can use awk/sed to read from the Error Example list ignoring the ": broken symbolic link to" and the uuid (/mnt/super/d8e15eda-666e-454b-9def-19bb69wr41ss/944c8f69-be2a-47ff-b18b-706a71cab85d/) and re-link it to the correct location: /mnt/example/FOLDER_2/DL081/master/super/XXX/XX/XX/


Error Example list

/mnt/example/FOLDER_1/master/2002/05/26/ABC_DE__FGGHJ20020526_152812_000050022006_00183_01239_0001.N1: broken symbolic link to `/mnt/super/d8e15eda-666e-454b-9def-19bb69wr41ss/944c8f69-be2a-47ff-b18b-706a71cab85d/ABC_DE__FGGHJ20020526_152812_000050022006_00183_01239_0001.N1'
/mnt/example/FOLDER_1/master/2005/02/25/ABC_DE__FGGHJ20050225_164634_000011352006_00183_01239_0001.N1: broken symbolic link to `/mnt/super/d8e15eda-666e-454b-9def-19bb69wr41ss/37e2d022-81d7-45ff-9bf7-d92a78675340/ABC_DE__FGGHJ20050225_164634_000011352006_00183_01239_0001.N1'
/mnt/example/FOLDER_1/master/2007/09/12/ABC_DE__FGGHJ20070912_170334_000053152006_00184_01240_0001.N1: broken symbolic link to `/mnt/super/d8e15eda-666e-454b-9def-19bb69wr41ss/7b9726b2-16cd-47c0-aaad-b985b6acf2f6/ABC_DE__FGGHJ20070912_170334_000053152006_00184_01240_0001.N1'

Correct symlinks Example

/mnt/example/FOLDER_1/master/2002/05/26/ABC_DE__FGGHJ20020526_152812_000050022006_00183_01239_0001.N1 -> /mnt/example/FOLDER_2/DL081/master/super/2002/05/26/ABC_DE__FGGHJ20020526_152812_000050022006_00183_01239_0001.N1
/mnt/example/FOLDER_1/master/2005/02/25/ABC_DE__FGGHJ20050225_164634_000011352006_00183_01239_0001.N1 -> /mnt/example/FOLDER_2/DL081/master/super/2005/02/25/ABC_DE__FGGHJ20050225_164634_000011352006_00183_01239_0001.N1
/mnt/example/FOLDER_1/master/2007/09/12/ABC_DE__FGGHJ20070912_170334_000053152006_00184_01240_0001.N1 -> /mnt/example/FOLDER_2/DL081/master/super/2007/09/12/ABC_DE__FGGHJ20070912_170334_000053152006_00184_01240_0001.N1

Link location -> Original location

/mnt/example/FOLDER_1/master/ -> /mnt/example/FOLDER_2/DL081/master/super/

awarded to kerncy

Crowdsource coding tasks.

1 Solution

Winning solution


here is my solution using bash script that you have to set in a file name

if [ "$#" -ne 2 ]; then
    echo "usage is 'sh symlinkPath newPath'"
files=`find $1 -type l -name "*"`
for f in $files
    `ln -sf $2/$f $1/$f`

You can launch it with command :

sh /mnt/example/FOLDER_1/master /mnt/example/FOLDER_2/DL081/master/super

It will list ALL the existing symlinks (broken or not) recursively in your input folder and replace them by a new symlink to the same filename but pointing to your new path folder. I have made quicktests on my side, but it would be better to test first on a small amount on data on your side.

Do not hesitate if you have any questions

Thanks a lot!
Biti Kesa 4 months ago