Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

My client tells me they have cloned a VM in VMWare of an Ubuntu Linux server. Now it's my job to get into all the files and find out what still has the old server name of "bishop" and change it to something else. Also, the IP address is changed and I need to search for that too.

How would you typically use find, grep, awk, or sed to find these files and then change them rapidly? In the end, I want to make a Bash script.

Of course, I'm not expecting you to tell me every file, but just want to know the technique for finding files with "x" in it and then switching that rapidly with "y".

share|improve this question
    
The fact that this is a VM is not an issue in respect of the question so I have removed the vmware tag –  Iain Jun 24 '11 at 23:48
    
Do you want to find files that contain the string 'bishop' and/or some ip address ? –  Iain Jun 24 '11 at 23:50

1 Answer 1

up vote 2 down vote accepted

I'd actually strongly suggest that you make this a TWO step operation:

  1. Find the files with the old hostname and old IP:

    find / -print | xargs egrep -l 'oldhost|10.10.10.10' > filelist

  2. Then edit the resulting list and remove any that you know one should not change.

  3. Then use the resulting list, put it in a shell script and doing something like the following command sequence using the filelist as input:

.

cp filename filename.20110624     # lets be safe!  
if test $? -ne 0
then
    echo 'filename copy bad'
    exit 1
fi

cat filename.20110624 | sed 's/oldhost/newhost/g
s/10.10.10.10/10.20.20.20/g' > filename # the newline between commands is needed

if test $? -ne 0
then
    echo edit failed
    cp filename.20110624 filename
    if test $? -ne 0
    then
        echo unable to restore filename
        exit 1
    fi
    exit 1
 fi

 exit 0
share|improve this answer
    
Maybe someday, stackoverflow will support alternating code and bulletpoint blocks, until then I added a "." to have some plain text so the formatting would come out right. I also added a comment to point out the newline between sed s/// commands is significant –  DerfK Jun 25 '11 at 0:13
    
thanks DerflK for your edits! I was trying to do what you did for me. –  mdpc Jun 25 '11 at 0:16

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.