2

I'm trying to modify the following in order to rename some symbolic links:

find /home/user/public_html/qa/ -type l \
  -lname '/home/user/public_html/dev/*' -printf \
  'ln -nsf $(readlink %p|sed s/dev/qa/) $(echo %p|sed s/dev/qa/)\n'\
 > script.sh

Unfortunately the -lname option does not work for HPUX. Do you know something equivalent that I can use?

Just to give you and idea of my problem, I want to change all the symbolic links inside a particular folder.

New Symbolic link --> /base/testusr/scripts 
Old Symbolic link --> /base/produsr/scripts

Now folder "A" contains more than 100 different files having soft links which I need to change in this manner.

kalpesh
  • 21

1 Answers1

1

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