Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here are my access logs those I want to backup.


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 Jun 17 '11 at 13:45

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

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
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
find access_log* -type f -execdir mv {} {}.bak \;
share|improve this answer

Your Answer


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.