Find the answer to your Linux question:
Results 1 to 5 of 5
Hi, Im looking for assistance to create a script to find and replace files. Probably best if I give you the background Our server uses a specific application which stores ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2010
    Posts
    2

    Arrow Find & Replace Script in Bash


    Hi,

    Im looking for assistance to create a script to find and replace files. Probably best if I give you the background

    Our server uses a specific application which stores user data, each user data account (a folder on the server) has a file called 'Profile.xml' this file gets updated and replaced about every 30 mins similar to the fashion logrotate works i.e. Profile.xml.1 Profile.xml.2 -> .10

    What we experience is that if the application crashes unexpectedly while it is doing its user profile refresh task we end up with sometimes a few hundred Profile.xml files which end up 0kb(should be around 4kb) , and our server see's these as corrupted profiles and will not see them. Our fix is to go back thru and rename the Profile.xml.1 to be Profile.xml (or sometimes up to Profile.xml.5 to Profile.xml)

    We want a script we can manually run to automate this process

    The server tree is

    /mnt/array1/username/db/Profile.xml
    /mnt/array2/username/db/Profile.xml

    etc
    etc


    What we have so far is a script which finds the affected files


    find /mnt/ -maxdepth 4 -name Profile.xml -size -1k

    This will display a list of affected profiles, and we can append it to a text file with >>output.txt on the end.

    ive investigated a few options using cp, mv, or sed and none seem to achieve this easily

    perhaps some kind of script that uses a if statement?

    like

    if 'pattern' in 'location' equals '0kb' then 'cp' Profile.xml.1 Profile.xml

    sorry my scripting is not so good, can anyone make a suggestion?



    any help appreciated

  2. #2
    Just Joined!
    Join Date
    Jan 2010
    Posts
    12
    maybe you could try like this

    Code:
    ALLFILES=`find /mnt/ -maxdepth 4 -name Profile.xml -size -1k`
    
    while READ FILE; do
    
       cp $FILE".1" $FILE
    
    done < $ALLFILES
    this is not so good but it is goot to be an example

  3. #3
    Just Joined!
    Join Date
    Sep 2010
    Posts
    6
    here is my script, hope it helps:

    #! /bin/bash

    if [ $# -ne 3 ]
    then
    echo "Usage - $0 file target replacement"
    exit 1
    fi

    sed -e "s/$2/$3/g" $1 > $1__new

    echo "$1__new Done"
    On my machine I have a pretty good utility called replace.
    Take a look to the Linux man page.
    Last edited by aihaike; 09-28-2010 at 06:44 AM.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Sep 2010
    Posts
    2
    Quote Originally Posted by zgrw View Post
    maybe you could try like this

    Code:
    ALLFILES=`find /mnt/ -maxdepth 4 -name Profile.xml -size -1k`
    
    while READ FILE; do
    
       cp $FILE".1" $FILE
    
    done < $ALLFILES
    this is not so good but it is goot to be an example

    Thanks

    Doesnt appear to work with ambiguous redirects

    ./profilefix: line 9: $ALLFILES: ambiguous redirect

  6. #5
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    This assumes that Profile.xml.1 is not empty. If necessary, loop over Profile.xml.x, test for size of Profile.xml.x before restoring.

    Code:
    ALLFILES=$(find /mnt/ -maxdepth 4 -name "Profile.xml" -size -1k)
    
    for f in $ALLFILES
    do
     cp -f "$f.1" $f
    done
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •