Created
February 2, 2024 23:58
-
-
Save Milvintsiss/aed7ee7ff7ee1ec03b4f63aa928f33ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Adaptation of: https://stackoverflow.com/questions/31020219/how-change-symlink-path-for-many-files | |
run_mode=false | |
[ $# -eq 3 ] && [ $3 = "run" ] && run_mode=true | |
if [ $# -ne 2 ] && [ "$run_mode" = false ]; then | |
echo "Usage:"; | |
echo "- dry-run: ./scriptname oldpath newpath"; | |
echo "- run: ./scriptname oldpath newpath run"; | |
exit -1; | |
fi | |
oldpath=$1 | |
newpath=$2 | |
error_file="/tmp/$(basename $0).log" | |
echo "******************" | |
echo "old path: $oldpath" | |
echo "new path: $newpath" | |
echo "error file: $error_file" | |
if [ "$run_mode" = false ]; then | |
echo "running in dry-run mode." | |
fi | |
echo -e "******************\n" | |
find . -type l -execdir bash -c 'p="$(readlink "{}")"; if [ "${p:0:1}" != "/" ]; then p="$(echo "$(pwd)/$p" | sed -e "s|/\./|/|g" -e ":a" -e "s|/[^/]*/\.\./|/|" -e "t a")"; fi; if [ "${p:0:'${#oldpath}'}" == "'"$oldpath"'" ]; then \ | |
echo "-----"; \ | |
echo "pwd: "$(pwd); \ | |
echo "link name: ""{}"; \ | |
echo "old link: "$p; \ | |
echo "new link: ""'"$newpath"'${p:'${#oldpath}'}"; \ | |
if [ '"$run_mode"' = true ]; then \ | |
ln -sf "'"$newpath"'${p:'${#oldpath}'}" "{}"; \ | |
echo "Done."; \ | |
fi; \ | |
echo -e "-----\n"; \ | |
fi;' \; 2> $error_file | |
echo "$(cat $error_file | wc -l) errors. You can find the logs in $error_file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment