Search code examples
sftpscpfile-rename

Rename files while copying


In our unix server, the 'myfolder' contains many archive folders appended by date

archive.2012-04-10
archive.2012-04-11

Each archive folder contains a zipped file like below

my_transaction.log-20120410.gz

I know that the below one command will copy my_transaction.log-20120410.gz, my_transaction.log-20120411.gz and my_transaction.log-20120412.gz and put it under /tmp folder under 'serverip' server.

scp /myfolder/archive.2012-04-1[0-2]/my_transaction* username@serverip:/tmp

My question has two parts

1)What would be the one line command if I wanted to copy my_transaction.log-20120409.gz as well along with the above?

2)What would be the command to copy and rename the copied files under /tmp folder. That is it should be copied as below under /tmp. I would like to add an _1 with transaction while copied to 'serverip' /tmp folder.

my_transaction_1.log-20120409.gz
my_transaction_1.log-20120410.gz
my_transaction_1.log-20120411.gz
my_transaction_1.log-20120412.gz

Solution

  • For the first point, try extended globbing: (untested)

    # This could go in your ~/.bashrc
    shopt -s extglob
    scp /myfolder/archive.2012-04-@(09|1[0-2])/my_transaction* username@serverip:/tmp
    

    For the second point, I don't think you can both scp and rename all the files in one go. You could do them all in a loop, though. E.g. (also untested)

    for file in /myfolder/archive.2012-04-@(09|1[0-2])/my_transaction*; do
        newfile=$( echo $file | sed -e "s/my_transaction/my_transaction_1/")
        # Take out the "echo" if the command looks OK
        echo scp $newfile username@serverip:/tmp
    done