With only POSIX tools, the only way to see the target of a symbolic link is through ls
. The Linux and BSD readlink
command is unfortunately not standard.
Using ls
is brittle because you have to parse out the file names. Assuming that your file names do not contain newlines and that the targets of the symlinks do not contain the substring ->
, the command ls -l "$link" | sed 's/.* -> //'
prints the target of the link.
find /home/user/public_html/qa/ -type l | while IFS= read -r link; do target=$(ls -l "$link" | sed 's/.* -> //') case $target in /home/user/public_html/dev/*) link_to_change=$(echo "$link" | sed s/dev/qa/) ln -nsf "$(echo "$target" | sed s/dev/qa/)" "$link_to_change";; esac done