Here are my access logs those I want to backup.

/var/log/httpd/access_log
/var/log/httpd/access_log.1
/var/log/httpd/access_log.2
/var/log/httpd/access_log.3 
...

I want to copy all these files but with different name:

The following does work as expected, but is it possible to write a single command assuming there will be a lot of files to copy?

cp /var/log/httpd/access_log /home/shantanu/access_log.bak
cp /var/log/httpd/access_log.1 /home/shantanu/access_log.1.bak
link|improve this question

feedback

migrated from stackoverflow.com Jun 17 '11 at 13:45

This question came from our site for professional and enthusiast programmers.

3 Answers

up vote 1 down vote accepted

I think this should do the trick.

for f in /var/log/httpd/access_log*; do cp $f /home/shantanu/$(basename $f).bak; done
link|improve this answer
feedback
ls /var/log/httpd/access_log* | xargs -I% cp % %.bak
mv /var/log/httpd/*.bak /to/somewhere
link|improve this answer
Why wouldn't you put the mv command in xargs? If /var/log and /to/somewhere are on different volumes, you're writing the file twice. – glenn jackman Jun 17 '11 at 14:04
because, the % expanded as full path name, so mv % /somewhere/%.bak will expand as mv /var/log/access.log /somewhere/var/log/access.log.bak. Handling with basename and etc - this is much easier... – jm666 Jun 17 '11 at 22:10
feedback
find access_log* -type f -execdir mv {} {}.bak \;
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.