Visit Jeremy's Blog.
Go Back > Blogs > linux-related notes
User Name


Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
Rating: 2 votes, 4.00 average.

Batch-fixing symlink targets

Posted 08-28-2010 at 06:08 PM by the dsc
Updated 12-10-2016 at 05:32 PM by the dsc
Tags batch, symlink

Sometimes you move the folder to which lots of symlinks were pointing, or something to that effect, and you want to simply "edit" these symlinks rather than recreating them pointing the correct new location. That's how it can be done with one line:

find /place-where-the-null-symlinks are/ -type l | while read nullsymlink ; 
do wrongpath=$(readlink "$nullsymlink") ; 
right=$(echo "$wrongpath" | sed s'|/whatever/is/the/old-wrong/path|/correct-new/path|') ;
ln -fs "$right" "$nullsymlink" ; done
Of course, that will work only if the files the symlinks were pointing to were all moved together to some other location, preserving the folder structure after "/whatever/is/the/old-wrong/path" on the "/correct-new/path".

BEWARE: check THRICE for the correct order and variable attributions. If you accidentaly use "ln -fs $wrongpath $right", you simply INSTANTLY OVERWRITE THE ACTUAL FILES WITH EMPTY SYMLINKS.
Posted in Uncategorized
Views 6820 Comments 1
« Prev     Main     Next »
Total Comments 1


  1. Old Comment
    This works like a charm; took just a second processing in a directory with a few dozen broken symlinks :-))
    Thank you the_dsc for sharing your knowledge.
    Posted 12-11-2012 at 08:25 AM by kozaki kozaki is offline


All times are GMT -5. The time now is 05:40 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration