Bash

複製和重命名文件

  • June 17, 2011

這是我要備份的訪問日誌。

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

我想複製所有這些文件,但名稱不同:

以下確實按預期工作,但是否可以編寫一個命令假設將有很多文件要複製?

cp /var/log/httpd/access_log /home/shantanu/access_log.bak
cp /var/log/httpd/access_log.1 /home/shantanu/access_log.1.bak

我認為這應該可以解決問題。

for f in /var/log/httpd/access_log*; do cp $f /home/shantanu/$(basename $f).bak; done
ls /var/log/httpd/access_log* | xargs -I% cp % %.bak
mv /var/log/httpd/*.bak /to/somewhere

引用自:https://serverfault.com/questions/281511