批量文件改名案例实战
批量文件改名案例实战:
把下面所有文件的文件名中的finished内容去掉
[root@XCNtest]#lltotal0-rw-r--r--1rootroot0Jun2010:25xcn_1_finished.jpg-rw-r--r--1rootroot0Jun2010:25xcn_2_finished.jpg-rw-r--r--1rootroot0Jun2010:25xcn_3_finished.jpg-rw-r--r--1rootroot0Jun2010:25xcn_4_finished.jpg-rw-r--r--1rootroot0Jun2010:29xcn_5_finished.jpg-rw-r--r--1rootroot0Jun2010:26xcn_6_finished.jpg
方法1:shell脚本for循环结合sed实现
#!/bin/bashforfilein`ls./*.jpg`#shell脚本for循环,file为变量依次取得ls./*.jpg的结果文件名domv$file`echo$file|sed's/finished//g'`#使用mv命令进行更改文件,新的文件名字符串拼接是本题的重点。done执行后结果[root@XCNtest]#lltotal4-rw-r--r--1rootroot87Jun2010:35finished.sh-rw-r--r--1rootroot0Jun2010:25xcn_1_.jpg-rw-r--r--1rootroot0Jun2010:25xcn_2_.jpg-rw-r--r--1rootroot0Jun2010:25xcn_3_.jpg-rw-r--r--1rootroot0Jun2010:25xcn_4_.jpg-rw-r--r--1rootroot0Jun2010:29xcn_5_.jpg-rw-r--r--1rootroot0Jun2010:26xcn_6_.jpg
方法二:shell脚本for循环加变量的部分截取方法
#!/bin/bash#xcnqq:995345781forfilein`ls./*.jpg`do/bin/mv$file`echo"${file%finished*}.jpg"`#这里就是变量的截取新方法done执行后结果:[root@XCNtest]#lltotal4-rw-r--r--1rootroot108Jun2010:41change_file_name.sh-rw-r--r--1rootroot0Jun2010:38xcn_1_.jpg-rw-r--r--1rootroot0Jun2010:38xcn_2_.jpg-rw-r--r--1rootroot0Jun2010:39xcn_3_.jpg-rw-r--r--1rootroot0Jun2010:39xcn_4_.jpg-rw-r--r--1rootroot0Jun2010:38xcn_5_.jpg-rw-r--r--1rootroot0Jun2010:38xcn_6_.jpg
方法三:ls结合awk实现
[root@XCNtest]#lltotal0-rw-r--r--1rootroot0Jun2010:44xcn_1_finished.jpg-rw-r--r--1rootroot0Jun2010:44xcn_2_finished.jpg-rw-r--r--1rootroot0Jun2010:44xcn_3_finished.jpg-rw-r--r--1rootroot0Jun2010:44xcn_4_finished.jpg-rw-r--r--1rootroot0Jun2010:44xcn_5_finished.jpg-rw-r--r--1rootroot0Jun2010:44xcn_6_finished.jpg[root@XCNtest]#ls|awk-F'finished''{print$0}'xcn_1_finished.jpgxcn_2_finished.jpgxcn_3_finished.jpgxcn_4_finished.jpgxcn_5_finished.jpgxcn_6_finished.jpg[root@XCNtest]#ls|awk-F'finished''{print$1}'xcn_1_xcn_2_xcn_3_xcn_4_xcn_5_xcn_6_[root@XCNtest]#ls|awk-F'finished''{print$2}'.jpg.jpg.jpg.jpg.jpg.jpg[root@XCNtest]#ls|awk-F'finished''{print$1$2}'xcn_1_.jpgxcn_2_.jpgxcn_3_.jpgxcn_4_.jpgxcn_5_.jpgxcn_6_.jpg[root@XCNtest]#ls|awk-F'finished''{print"mv"$0""$1$2""}'|/bin/bash[root@XCNtest]#lltotal0-rw-r--r--1rootroot0Jun2010:44xcn_1_.jpg-rw-r--r--1rootroot0Jun2010:44xcn_2_.jpg-rw-r--r--1rootroot0Jun2010:44xcn_3_.jpg-rw-r--r--1rootroot0Jun2010:44xcn_4_.jpg-rw-r--r--1rootroot0Jun2010:44xcn_5_.jpg-rw-r--r--1rootroot0Jun2010:44xcn_6_.jpg[root@XCNtest]#
方法四:通过专业的改名命令rename实现
[root@XCNtest]#lltotal0-rw-r--r--1rootroot0Jun2010:59xcn_1_finished.jpg-rw-r--r--1rootroot0Jun2010:59xcn_2_finished.jpg-rw-r--r--1rootroot0Jun2010:59xcn_3_finished.jpg-rw-r--r--1rootroot0Jun2010:59xcn_4_finished.jpg-rw-r--r--1rootroot0Jun2010:59xcn_5_finished.jpg-rw-r--r--1rootroot0Jun2010:58xcn_6_finished.jpg[root@XCNtest]#rename"finished"""*[root@XCNtest]#lltotal0-rw-r--r--1rootroot0Jun2010:59xcn_1_.jpg-rw-r--r--1rootroot0Jun2010:59xcn_2_.jpg-rw-r--r--1rootroot0Jun2010:59xcn_3_.jpg-rw-r--r--1rootroot0Jun2010:59xcn_4_.jpg-rw-r--r--1rootroot0Jun2010:59xcn_5_.jpg-rw-r--r--1rootroot0Jun2010:58xcn_6_.jpg
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。