#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    1
    Rep Power
    0

    How to create simple Copy and Over write Script


    I am trying to create a script that would copy file from multiple locations to another location recursively. Such as this:

    Copy the contents of:


    Code:
    /var/router/system1/config/backup/install.put
    /var/router/system2/config/backup/install.put
    /var/router/system3/config/backup/install.put
    /var/router/system4/config/backup/install.put
    Over Write :

    Code:
    /var/router/system1/config/install.dat
    /var/router/system2/config/install.dat
    /var/router/system3/config/install.dat
    /var/router/system4/config/install.dat
    In this way, install.put becomes install.dat in config directory.

    --

    I tried to do run the below script command, first by creating a file called 'infile' with:

    Code:
    /var/router/system1/config/backup/install.put
    /var/router/system2/config/backup/install.put
    /var/router/system3/config/backup/install.put
    /var/router/system4/config/backup/install.put
    then use this command:
    Code:
    nawk -F'[/.]' 'NF{print "cp " $0 " " $0 "/../"$(NF-1)".dat"}' infile
    however the above command is not over writing as i had wanted.

    Any ideas?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481

    Use bash


    Code:
    pushd /var/router/system4/config/backup
    for f in *.put ; do echo /bin/cp --force $f ../${f%put}dat ; done
    popd
    • I'm hoping that using /bin/cp --force will avoid problems with NOCLOBBER shell variable setting. Or whatever. I haven't used it.
    • Do you have permission for this operation?
    • Once you verify the cp commands to execute you can either pipe the for ... done | bash or remove echo



    Or, working with your nawk program, you could change cp to $( which cp ) --force and pipe the output to sh
    Last edited by b49P23TIvg; October 1st, 2011 at 03:20 PM. Reason: more info

IMN logo majestic logo threadwatch logo seochat tools logo