Solved

Need script to rebuild soft links

Posted on 2000-04-12
4
261 Views
Last Modified: 2010-04-21
Here's the problem,

When I refesh a developement environment from a tape backup of production, there are a lot of softlinks in many different subdirectories that need to be rebuilt, so it links to the right files.

Can I get a script that can take the directory name that needs changed, the directory name that it needs changed to, and the full path to the directory so any subdirectorieswith bad links will get fixed.

here's an example of what I'm talking about...

in

/ORACLE/apps/DEV1/eper/1.0.0/bin

there are many links like

lrwxrwxrwx   1 applmgr    apps            57 Mar 30 13:05 HRPSRSRE -> /ORACLE/apps/PROD1/fnd/6.1.1/bin/fndcpesr

Basically I need a quick way of changing all links from PROD1 to DEV1 (The sub-directory structures are the same between production and developement)

Ideally, the command would look something like

change_links.sh PROD1 DEV1 /ORACLE/apps/DEV1

I'm running on HPUX 10.20 if that helps.

If I am unclear about anything please let me know.

And please post comments, not answers, I will accept your comment as an answer, when I verify that everything works.


0
Comment
Question by:mboss
  • 2
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
tfewster earned 250 total points
ID: 2708229
change_links.sh:

#!/bin/sh
# Usage: change_links.sh OLDDIR NEWDIR LINK

OLDDIR=$1
NEWDIR=$2
LINK=$3

# Check links
echo
echo Link $LINK
echo `ls -l $LINK | awk '{ print $9 $10 $11}'`
echo becomes
echo `ls -l $LINK | awk '{ print $ 11}' | sed s/$OLDDIR/$NEWDIR/g`

# Change links
#ln -f -s `ls -l $LINK | awk '{ print $ 11}' | sed s/$OLDDIR/$NEWDIR/g` $LINK


To use this, call it from a "find", e.g.
find /oracle/apps/DEV1 -type l -exec ~/test/change_links.sh PROD1 DEV1 {} \;

Note that it just tests the links for now, & echos what it finds + what it WOULD do when you
uncomment the "ln ..." line

It will go wrong if you have a directory PROD1 under DEV1...
0
 
LVL 20

Expert Comment

by:tfewster
ID: 2708333
I've put an addditional check in, so if the links found don't contain the string $OLDDIR, they are just ignored (My previous script would have rewritten them, which is pointless & asking for trouble...)

change_links.sh:

#!/bin/sh
# Usage: change_links.sh OLDDIR NEWDIR LINK

OLDDIR=$1
NEWDIR=$2
LINK=$3

#test if $LINK contains string $OLDDIR
TARGET=`ls -l $LINK | awk '{ print $ 11}'|grep $OLDDIR`
echo $TARGET

if [ ! X$TARGET = X ]
then
  # Check links
  echo
  echo Link $LINK
  echo `ls -l $LINK | awk '{ print $9 $10 $11}'`
  echo becomes
  echo `ls -l $LINK | awk '{ print $ 11}' | sed s/$OLDDIR/$NEWDIR/g`

  # Change links
  #ln -f -s `ls -l $LINK | awk '{ print $11}' | sed s/$OLDDIR/$NEWDIR/g` $LINK
fi
0
 

Author Comment

by:mboss
ID: 2708357
Excellent.  Thanks for all your help.  Can you recommend a good book/site to learn sed and awk?  

I raised the points to 300 cause you deserve it.

Thanks again

mboss
0
 

Author Comment

by:mboss
ID: 2708370
woops...I think I forgot to rasie the points like I said I would.  I'll post them for you an a sec.

Sorry about that.

mboss
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now