shell批量修改文件名的方法:
[oracle@warehouse tmp]$ ll
total 0
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111125.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111126.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111127.log
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$ find . -type f
./run20111127.log
./run20111125.log
./run20111126.log
[oracle@warehouse tmp]$
增加后缀:
[oracle@warehouse tmp]$ find . -type f|xargs -t -i mv {} {}_postfix
mv ./run20111127.log ./run20111127.log_postfix
mv ./run20111125.log ./run20111125.log_postfix
mv ./run20111126.log ./run20111126.log_postfix
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$ find . -type f
./run20111127.log_postfix
./run20111125.log_postfix
./run20111126.log_postfix
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$ ll
total 0
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111125.log_postfix
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111126.log_postfix
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111127.log_postfix
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$
去掉后缀:
[oracle@warehouse tmp]$ find . -type f|xargs -t rename _postfix ""
rename _postfix ./run20111127.log_postfix ./run20111125.log_postfix ./run20111126.log_postfix
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$ ll
total 0
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111125.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111126.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111127.log
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$ find . -type f
./run20111127.log
./run20111125.log
./run20111126.log
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$
增加前缀:
[oracle@warehouse tmp]$ find . -type f|xargs -t rename ./ prefix_
rename ./ prefix_ ./run20111127.log ./run20111125.log ./run20111126.log
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$ ll
total 0
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 prefix_run20111125.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 prefix_run20111126.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 prefix_run20111127.log
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$ find . -type f
./prefix_run20111126.log
./prefix_run20111127.log
./prefix_run20111125.log
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$
去掉前缀:
[oracle@warehouse tmp]$ find . -type f|xargs -t rename prefix_ ""
rename prefix_ ./prefix_run20111126.log ./prefix_run20111127.log ./prefix_run20111125.log
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$ ll
total 0
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111125.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111126.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111127.log
同理修改文件名中部分字符串:
[oracle@warehouse tmp]$ find . -type f|xargs -t rename run "test"
rename run test ./run20111127.log ./run20111125.log ./run20111126.log
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$ ll
total 0
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 test20111125.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 test20111126.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 test20111127.log
[oracle@warehouse tmp]$
如果文件名有规则,可以直接这样改:
[oracle@warehouse tmp]$ rename test run *.log
[oracle@warehouse tmp]$ ll
total 0
-rw-r--r-- 1 oracle oinstall 0 Oct 25 11:04 run20111125.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 11:04 run20111126.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 11:04 run20111127.log
[oracle@warehouse tmp]$ ll
total 0
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111125.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111126.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111127.log
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$ find . -type f
./run20111127.log
./run20111125.log
./run20111126.log
[oracle@warehouse tmp]$
增加后缀:
[oracle@warehouse tmp]$ find . -type f|xargs -t -i mv {} {}_postfix
mv ./run20111127.log ./run20111127.log_postfix
mv ./run20111125.log ./run20111125.log_postfix
mv ./run20111126.log ./run20111126.log_postfix
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$ find . -type f
./run20111127.log_postfix
./run20111125.log_postfix
./run20111126.log_postfix
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$ ll
total 0
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111125.log_postfix
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111126.log_postfix
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111127.log_postfix
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$
去掉后缀:
[oracle@warehouse tmp]$ find . -type f|xargs -t rename _postfix ""
rename _postfix ./run20111127.log_postfix ./run20111125.log_postfix ./run20111126.log_postfix
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$ ll
total 0
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111125.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111126.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111127.log
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$ find . -type f
./run20111127.log
./run20111125.log
./run20111126.log
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$
增加前缀:
[oracle@warehouse tmp]$ find . -type f|xargs -t rename ./ prefix_
rename ./ prefix_ ./run20111127.log ./run20111125.log ./run20111126.log
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$ ll
total 0
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 prefix_run20111125.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 prefix_run20111126.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 prefix_run20111127.log
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$ find . -type f
./prefix_run20111126.log
./prefix_run20111127.log
./prefix_run20111125.log
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$
去掉前缀:
[oracle@warehouse tmp]$ find . -type f|xargs -t rename prefix_ ""
rename prefix_ ./prefix_run20111126.log ./prefix_run20111127.log ./prefix_run20111125.log
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$ ll
total 0
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111125.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111126.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 run20111127.log
同理修改文件名中部分字符串:
[oracle@warehouse tmp]$ find . -type f|xargs -t rename run "test"
rename run test ./run20111127.log ./run20111125.log ./run20111126.log
[oracle@warehouse tmp]$
[oracle@warehouse tmp]$ ll
total 0
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 test20111125.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 test20111126.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 10:43 test20111127.log
[oracle@warehouse tmp]$
如果文件名有规则,可以直接这样改:
[oracle@warehouse tmp]$ rename test run *.log
[oracle@warehouse tmp]$ ll
total 0
-rw-r--r-- 1 oracle oinstall 0 Oct 25 11:04 run20111125.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 11:04 run20111126.log
-rw-r--r-- 1 oracle oinstall 0 Oct 25 11:04 run20111127.log
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12355989/viewspace-709635/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/12355989/viewspace-709635/