0

I want to make a cronjob that makes a copy of every file in /var/log, and puts it in /etc/bak. That i do by making an sh file saying

cp /var/log/*.* /etc/bak

I would like these files not to be overwritten, but to be unique. To do this i figured i want to put the date/time in the filename. Is there any way to make that possible on a batch file copy?

2 Answers 2

2

How about putting every batch of log files in a separate directory. You could do it using:

dir=/etc/bak/`date "+%Y-%m-%d_%H.%M.%S"`
mkdir "$dir"
cp /var/log/*.* $dir

If you want to prefix all the copied files with a timestamp you can do:

(cd /var/log && ls -1 *.*) | awk -v date=`date "+%Y-%m-%d_%H.%M.%S"` '{ print "cp /var/log/" $0 " /etc/bak/" date  $0}' | sh
0

To copy files to another directory and change their names in the progress, I'd recommend using a for-loop.

The following code extracts the file's name using basename, then creates a copy of that file in /etc/bak named like the original file, with an attached date.

for file in /var/log/*; do 
    bn=$(basename $file)
    cp $file /etc/bak/${bn}.$(date +%F)
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .