javaguy Posted February 10, 2004 Report Share Posted February 10, 2004 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? Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted February 10, 2004 Report Share Posted February 10, 2004 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. :-) Quote Link to comment Share on other sites More sharing options...
neutro Posted February 10, 2004 Report Share Posted February 10, 2004 Would using "cp -s" help you? You just copy the files you want to another location, but instead of making copies, this makes symlinks. Quote Link to comment Share on other sites More sharing options...
javaguy Posted February 10, 2004 Author Report Share Posted February 10, 2004 (edited) 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 February 10, 2004 by javaguy Quote Link to comment Share on other sites More sharing options...
javaguy Posted February 10, 2004 Author Report Share Posted February 10, 2004 (edited) 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. Edited February 10, 2004 by javaguy Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.