0

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
shantanuo
  • 3,459
  • 8
  • 47
  • 64

3 Answers3

2

I think this should do the trick.

for f in /var/log/httpd/access_log*; do cp $f /home/shantanu/$(basename $f).bak; done
Chris Eberle
  • 285
  • 1
  • 6
1
ls /var/log/httpd/access_log* | xargs -I% cp % %.bak
mv /var/log/httpd/*.bak /to/somewhere
clt60
  • 414
  • 3
  • 10
  • 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... – clt60 Jun 17 '11 at 22:10
0
find access_log* -type f -execdir mv {} {}.bak \;
Mike
  • 21,910
  • 7
  • 55
  • 79