Modify several symlinks at once

adding ../ to the target path

If you have to add the same property, i.g. ../ to several symlinks in the same folder, you can use this code:

#!/bin/bash

for f in *; do 
  [ -L "$f" ] || continue
  [ -e "$f" ] && continue   # skip the valid ones

  target=$(readlink "$f")
  [[ "$target" != /* ]] && ln -snf ../"$target" "$f"
done

You can either save this code in a bash file, or paste it directly in a terminal.

another case: replace the name of the target file

If you have now target files renamed with “-” instead of space ” “, you can use this code:

for f in *; do
  [ -L "$f" ] || continue
  [ -e "$f" ] && continue

  target=$(readlink "$f")
  newtarget=${target// /-}

  [[ "$target" != "$newtarget" ]] && ln -snf "$newtarget" "$f"
done

Lascia un commento

Il tuo indirizzo email non sarĂ  pubblicato. I campi obbligatori sono contrassegnati *