Søren Lund (slu) wrote,
Søren Lund
slu

  • Location:
  • Mood:

Updating softlinks after moving the files they were pointing at

I'm currently doing some cleanup work on my main desktop computer. Not only is this "a good thing" in general, I'm also planning on replacing my Fedora Core Linux with a Ubuntu Linux. Although the latter does not require it, I'm planning on re-formatting the disk when installing Ubuntu.

Well, during my cleanup, I've shuffled my photos around a bit. The result is better, but today I stumbled across a directory that contains softlinks to a directory containing photos. I'd moved the (target) directory, thus none of the softlinked image files were readable.

How could I update the softlinks to point the the new location?

I ended up with this solution:

$ for f in *.JPG; do test -L $f && test -f /new/path/$f && rm $f && ln -s /new/path/$f .; done


obviously you should replaces /new/path with the new path to the files.

Here's a "script" version of the above one-liner:

for f in *.JPG; do
  test -L $f && \
  test -f /new/path/$f && \
  rm $f && \
  ln -s /new/path/$f .
done


I'm looping over all files with the extension .JPG in the current directory. For each file i check if it's a softlink (test -L $f) and a file with the same name exists in the new location (test -f /new/path/$f). If both checks are true the softlink is removed (rm $f) and a new softlink is created (ln -s /new/path/$f .)

Pretty simple, eh?
Tags: shell softlinks
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your IP address will be recorded 

  • 0 comments