Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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
share|improve this question

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

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

3 Answers 3

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
share|improve this answer
find access_log* -type f -execdir mv {} {}.bak \;
share|improve this answer
ls /var/log/httpd/access_log* | xargs -I% cp % %.bak
mv /var/log/httpd/*.bak /to/somewhere
share|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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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