Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
Addressing a question to a particular user doesn't work. –  Dennis Williamson Dec 27 '10 at 20:29
    
thanks for editing Dennis, as the script I was referring to was from David so mentioned his name. –  kalpesh Dec 28 '10 at 12:43
    
hi All, my issue got resolved with the following solution "find . -type l -exec ll -a {} + | awk '{ print "ln -sf", $11, $9}' > script" –  kalpesh Dec 28 '10 at 13:58
add comment

1 Answer

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.