#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Ireland
    Posts
    5
    Rep Power
    0

    Post Unix Script "File Change"


    Hi Guys,

    I am looking for a script that will change a certain character of a file name. I have a bunch of files from a location i.e "wf95234red3"
    and i want to change the file to "wf96234red3" just changing the 6 . What i need is to change lots of these at the same time.

    All the files have different letters at the end but the first part is the same wf95.

    Regards Trouble........
  2. #2
  3. Full Access
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jun 2000
    Location
    London, UK
    Posts
    2,019
    Rep Power
    17
    Alex
    (http://www.alex-greg.com)
  4. #3
  5. funky munky
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2001
    Location
    UK
    Posts
    1,446
    Rep Power
    16
    A small shell script should do the trick if you only want to change the 5 to a 6 (run this in the same directory as the wf95* files):

    Code:
    #!/bin/sh
    for file in `ls wf95*`
    do
    echo $file | sed -e 's/wf95\(.*\)/mv "&" wf96\1/' | sh
    done
    You might want to test it first by removing the '| sh' - it should output a list of 'mv' commands:

    Code:
    [23:47:22] root@users /root/tmp# ./tmp.sh
    mv wf95234red3 wf96234red3
    Even quicker you could just run the command from the commandline:
    Code:
    ls -1 wf95* | sed -e 's/wf95\(.*\)/mv "&" wf96\1/' | sh
    again though make sure the command is outputting what it should by removing the |sh first.
    Last edited by munkfish; August 17th, 2003 at 09:23 AM.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Ireland
    Posts
    5
    Rep Power
    0
    Hi Munkfish,

    That works fine, But i have a further problem.

    I will also need to change the same thing inside the file also.

    Regards Barry....
  8. #5
  9. funky munky
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2001
    Location
    UK
    Posts
    1,446
    Rep Power
    16
    Use the -i switch to edit the file 'in place':

    Code:
    sed -i -e 's/wf95/wf96/g' wf96*
    To change every occurrence of 'wf95' with 'wf96' everywhere it occurs on each line.

IMN logo majestic logo threadwatch logo seochat tools logo