Script to backup multiple files and amend their filenames


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to backup multiple files and amend their filenames
# 1  
Old 11-15-2005
Script to backup multiple files and amend their filenames

Hi,

I'm trying to write a command that backs up certain files into my current directory and adds a prefix to the backed up file name. I realise this can be done in a script by specifying each individual file but would like to know if it can be done on one line and made an alias.

I have the following files
AMQERR01.LOG
AMQERR02.LOG
AMQERR03.LOG

and I want to create backups called
$QM-AMQERR01.LOG
$QM-AMQERR02.LOG
$QM-AMQERR03.LOG

I've been using this command
find /var/mqm/qmgrs/$QM/errors -name "AMQERR*" -exec cp {} $QM-* \;
but the cp command creates one file called, e.g. QMNAME-*

I also tried
find /var/mqm/qmgrs/$QM/errors -name "AMQERR*" -exec cp {} $QM-{} \;
but the file was renamed QMNAME-{}

Is this possible?

Regards

Gareth
# 2  
Old 11-15-2005
Two ways:
1. Backup all files in a single directory:
Code:
for QM in (specify list of queue managers here); do
   cd /var/mqm/qmgrs/$QM/errors
   for file in AMQERR*; do
      cp -p $file /central_dir_path/$QM-$file;
   done
done

2. Backup files in their own directory:
Code:
for QM in (specify list of queue managers here); do
   cd /var/mqm/qmgrs/$QM/errors
   for file in AMQERR*; do
      cp -p $file $QM-$file
   done
done

Note: not tested
# 3  
Old 11-16-2005
Quote:
Originally Posted by m223464
I'm trying to write a command that backs up certain files into my current directory and adds a prefix to the backed up file name. I realise this can be done in a script by specifying each individual file but would like to know if it can be done on one line and made an alias.
...
Is this possible?
Yup. Try this:

Code:
alias doit='find . -name "AMQERR*" -print | sed -e "s#\.\/##" | xargs -l -i cp {} \$QM-{}'

...Note that this code will descend into any subdirectories, which may not be what you want. Substitute a directory for dot, if you want (and maybe exclude the sed command).
-mschwage
# 4  
Old 11-16-2005
Quote:
Originally Posted by mschwage
Yup. Try this:

Code:
alias doit='find . -name "AMQERR*" -print | sed -e "s#\.\/##" | xargs -l -i cp {} \$QM-{}'

...Note that this code will descend into any subdirectories, which may not be what you want. Substitute a directory for dot, if you want (and maybe exclude the sed command).
-mschwage
Thanks for both these suggestions. I've tried this one liner and am hitting a problem. I don't think the sed command is working as I was getting an error. I've amended the code as follows to try and debug it:

Code:
alias doit='find /var/mqm/qmgrs/$QM/errors -name "AMQERR*" -print | sed -e "s#\.\/##" | xargs -l -i echo $QM-{}'

The result is as follows:

STPFAUQA-/var/mqm/qmgrs/STPFAUQA/errors/AMQERR03.LOG
STPFAUQA-/var/mqm/qmgrs/STPFAUQA/errors/AMQERR02.LOG
STPFAUQA-/var/mqm/qmgrs/STPFAUQA/errors/AMQERR01.LOG

The output from the find is:
/var/mqm/qmgrs/STPFAUQA/errors/AMQERR03.LOG
/var/mqm/qmgrs/STPFAUQA/errors/AMQERR02.LOG
/var/mqm/qmgrs/STPFAUQA/errors/AMQERR01.LOG

What sed expressions should I use to change this to just the filename? Or alternatively is there an ls command which can just display the filename negating the need for the sed?

Regards

Gareth
# 5  
Old 11-16-2005
Ooo, that's a tough trick. You want to modify the value in {}, which I don't think can be done. Probably you should change the alias:

Code:
alias doit='(cd /var/mqm/qmgrs/$QM/errors; find . -name ......)'

...surround your commands with ()'s to run it in a subshell, then do a cd inside it. That will do the find in the new directory without putting you in the new directory. Make sure the ()'s are inside the single-quotes.

I prefer to use the . because an * can potentially fill the input buffer and create an invalid shell command. (Think about it for a second... the * matches all files in the current directory... if there are a lot of files, there will be a lot of matches. Whereas the find command's job is not to do shell metacharacter matching, it's to print out files that match your characteristics, line by line. This makes the dot safer to use with find.).
# 6  
Old 11-25-2005
Thanks for this tip. It almost worked but not quite as the output of the find was as below and the ./ caused problems.

./AMQERR03.LOG
./AMQERR02.LOG
./AMQERR01.LOG


In the end I used the original version and used cut to extract the file name. The command is pretty long but it works.
Code:
alias doit='find /var/mqm/qmgrs/$QM/errors -name "AMQERR*" -print |cut -d/ -f7| xargs -l -i cp /var/mqm/qmgrs/$QM/errors/{}
                 $HOME/backuplogs/$QM-$(date -u +%b)$(date -u +%d)-{}'

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

There are multiple filenames in the directory.How to return the the lastest files for each file name

there are mutiple file nams in the directory. How to return the the lastest files for each file name. ex. abc1234_050201 abc1234_050206 abc1234_050208 xyz34_050204 xyz34_050210 xyz34_050218 thanks (4 Replies)
Discussion started by: grand_sam
4 Replies

2. UNIX for Dummies Questions & Answers

Is there any way to cat multiple files and show filenames?

Hi, Is there any way to do a cat * where it shows the name of each file in the process? Similar to what more does below? $ more ?.sql :::::::::::::: 1.sql :::::::::::::: set linesize 200 select db_unique_name, cast( from_tz( cast(... (5 Replies)
Discussion started by: newbie_01
5 Replies

3. Shell Programming and Scripting

Run one script on multiple files and print out multiple files.

How can I Run one script on multiple files and print out multiple files. FOR EXAMPLE i want to run script.pl on 100 files named 1.txt ....100.txt under same directory and print out corresponding file 1.gff ....100.gff.THANKS (4 Replies)
Discussion started by: grace_shen
4 Replies

4. UNIX for Dummies Questions & Answers

Run one script on multiple files and print out multiple files.

How can I run the following command on multiple files and print out the corresponding multiple files. perl script.pl genome.gff 1.txt > 1.gff However, there are multiples files of 1.txt, from 1----100.txt Thank you so much. No duplicate posting! Continue here. (0 Replies)
Discussion started by: grace_shen
0 Replies

5. Shell Programming and Scripting

Script to compare substrings of multiple filenames and move to different directory

Hi there, I am having trouble with a script I have written, which is designed to search through a directory for a header and payload file, retrieve a string from both filenames, compare this string and if it matches make a backup of the two files then move them to a different directory for... (1 Reply)
Discussion started by: alcurry
1 Replies

6. Shell Programming and Scripting

extract multiple cloumns from multiple files; skip rows and include filenames; awk

Hello, I am trying to write a bash shell script that does the following: 1.Finds all *.txt files within my directory of interest 2. reads each of the files (25 files) one by one (tab-delimited format and have the same data format) 3. skips the first 10 rows of the file 4. extracts and... (4 Replies)
Discussion started by: manishabh
4 Replies

7. Shell Programming and Scripting

Script to Backup files

Hi, I wrote a simple script to backup of index.php and index.html in my box. So, I wrote a script which take a copy of the index page as 1Mar09: but it does not comes up.. #! /bin/bash find . -name index.* > domains.txt for i in `cat domains.txt` ; do cp index* index*.1Mar09 $i; done But... (6 Replies)
Discussion started by: gsiva
6 Replies

8. Shell Programming and Scripting

read list of filenames from text file and remove these files in multiple directories

I have a large list of filenames from an Excel sheet, which I then translate into a simple text file. I'd like to use this list, which contains various file extensions , to archive these files and then remove them recursively through multiple directories and subdirectories. So far, it looks like... (5 Replies)
Discussion started by: fxvisions
5 Replies

9. UNIX for Dummies Questions & Answers

How can I rename multiple files depending on a string occuring in the filenames?

I have many files that have "inputstring" somewhere in their filename (without the quotes), and I want to rename them all so that "inputstring" is replaced with "newstring". And I also want to specify arbitrary text for "inputstring" and "newstring" so that I can call the scripts that does this... (6 Replies)
Discussion started by: karman
6 Replies
Login or Register to Ask a Question