Jump to content

Change where a link points?


javaguy
 Share

Recommended Posts

I have a "gallery" directory containing links to a bunch of my favorite jpgs. I want to change the name of the directory that contains some of the actual files, but this will mean my links no longer point anywhere valid. Is there an efficient way I can update them at the command line, or do I just have to recreate them one at a time?

Link to comment
Share on other sites

This should work, but maybe you should back up your directory containing the links just in case like this:

 

cp -R /path/to/dir/containing/links /path/to/dir/containing/links-bak

 

Example:

My directory containing links is /home/omar/teststuff:

 

cp -R /home/omar/teststuff /home/omar/teststuff-bak

 

I place this script in /home/omar/teststuff:

#!/bin/sh

OLDLINK=/home/omar/blahlink
NEWLINK=/home/omar/newlink

DIR=$(ls)


for file in $DIR
  do
  TEST=$(readlink $file)
  if [[ $TEST ==  "$OLDLINK/$file" ]]
     then
     if [[ $file != "script" ]]
        then
        rm -f $file
        ln -s "$NEWLINK/$file" "$file"
     fi
  fi
  done

 

and name it script.

The old directory-name's path was /home/omar/blahlink and the new name's path is /home/omar/newlink (I'm assuming the name of the actual jpg in the directory is the same as the name you give the link).

 

cd /home/omar/teststuff

chmod +x script

./script

 

Disclaimer: If you don't completely understand this, don't blame me if it deletes important stuff. :-)

Link to comment
Share on other sites

I ran that script, and I think it's close, but no links get fixed. I added a couple of echo statements for debugging purposes:

 

#!/bin/sh

 

OLDLINK=" /home/javaguy/Documents/Photos/2003/2003 December/dcim/xmas2003a"

NEWLINK=" /home/javaguy/Documents/Photos/2003/2003 December/batch2"

 

DIR=$(ls)

 

 

for file in $DIR

do

TEST=$(readlink $file)

echo $OLDLINK/$file

echo $TEST

if [[ $TEST == "$OLDLINK/$file" ]]

then

echo "FOUND ONE: $file"

if [[ $file != "script" ]]

then

rm -f $file

ln -s "$NEWLINK/$file" "$file"

fi

fi

done

Here's part of the output I get:

 

/home/javaguy/Documents/Photos/2003/2003 December/dcim/xmas2003a/p1010103.jpg

/home/javaguy/Documents/Photos/2003/2003 December/dcim/xmas2003a/p1010103.jpg

/home/javaguy/Documents/Photos/2003/2003 December/dcim/xmas2003a/p1010106.jpg

/home/javaguy/Documents/Photos/2003/2003 December/dcim/xmas2003a/p1010106.jpg

/home/javaguy/Documents/Photos/2003/2003 December/dcim/xmas2003a/p1010113.jpg

/home/javaguy/Documents/Photos/2003/2003 December/dcim/xmas2003a/p1010113.jpg

/home/javaguy/Documents/Photos/2003/2003 December/dcim/xmas2003a/p1010121.jpg

/home/javaguy/Documents/Photos/2003/2003 December/dcim/xmas2003a/p1010121.jpg

/home/javaguy/Documents/Photos/2003/2003 December/dcim/xmas2003a/p1010122.jpg

/home/javaguy/Documents/Photos/2003/2003 December/dcim/xmas2003a/p1010122.jpg

 

Is there something about the comparison operator I don't understand? I'm very new to *nix shell scripting, but I do recall from other languages that comparison operators are one of the real "gotchas" for newbies.

Edited by javaguy
Link to comment
Share on other sites

Solved!

 

For some reason it adds a space at the beginning of $TEST, so the comparison has to say:

 

if [[ " $TEST" == "$OLDLINK/$file" ]]

 

 

Everything seems to have worked.

 

[edit/addendum]

On closer examination I see the reason for the space is that, when defining $OLDLINK, I put it there. :oops:

Edited by javaguy
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...