linux下批量修改文件名
0、問題來源,想把一部分文件加上日期序。也就是在文件名稱前加上當前的年月日進行輸出。
獲取日期格式如下
Zianed@ubuntu:~/public_web_sSmO9OUVY1/files/image$ date +%Y%m%d
20091112
1、使用find、awk/grep/sed、mv等命令組合來實現
Ubuntu下
利用awk命令進行製作,操作如下
1)找到需要進行操作的文件名
Zianed@ubuntu:~/public_web_sSmO9OUVY1/files/image$ find -name ‘*.jpg’ -printf %f//n|grep -v ^[0-9] |grep -v logo.jpg
se_platform.jpg
soa1.jpg
soa4.jpg
java_primitive_data_type.jpg
soa3.jpg
soa2.jpg
2)提取文件名,保持文件擴展名不變
Zianed@ubuntu:~/public_web_sSmO9OUVY1/files/image$ find -name ‘*.jpg’ -printf %f//n|grep -v ^[0-9] |grep -v logo.jpg|awk -F ‘.’ ‘{print $1}’
se_platform
soa1
soa4
java_primitive_data_type
soa3
soa2
3)對文件進行批量替換操作
Zianed@ubuntu:~/public_web_sSmO9OUVY1/files/image$ find -name ‘*.jpg’ -printf %f //n|grep -v ^[0-9] |grep -v logo.jpg|awk -F ‘.’ ‘{print $1}’|
xargs -i{} mv {}.jpg `date +%Y%m%d`{}.jpg
Zianed@ubuntu:~/public_web_sSmO9OUVY1/files/image$ ls
20090818a.jpg 20090919twitter4.jpg
20090818learn.jpg 20091110open_project.jpg
20090819java1.png 20091112java_primitive_data_type.jpg
20090819javaseplatformroadmap.gif 20091112se_platform.jpg
20090916neiji.jpg 20091112soa1.jpg
20090916waiji.jpg 20091112soa2.jpg
20090919photo.jpg 20091112soa3.jpg
20090919twitter.jpg 20091112soa4.jpg
20090919twitter2.jpg logo.jpg
20090919twitter3.jpg
利用nawk命令進行製作,操作如下
1)找到要操作的文件名
Zianed@ubuntu:~/sedcommand$ find ./ -type f | grep “log” |sed -n ’s//.////p’
test2.log
test1.log
2)進行文件名更改操作
Zianed@ubuntu:~/sedcommand$ ls *.log
test1.log test2.log
Zianed@ubuntu:~/sedcommand$ find ./ -type f | grep “log” |sed -n ’s/.////p’ |
nawk ‘{oldname=$1;$2=`date +%Y%m%d`$1;newname=$1;print “mv ” oldname ” ” newname }’ | /bin/sh
nawk: warning: escape sequence `/.’ treated as plain `.’
Zianed@ubuntu:~/sedcommand$ ls *.log
test1_test.log test2_test.log
Zianed@ubuntu:~/sedcommand$
利用sed命令進行製作,操作如下
find / -type f -name “*.sql” | sed s//(.*/)/.sql/mv & /1.pls/’ | sh
2、編寫Shell腳本運行
保存爲一個Shell腳本
#!/bin/bash
#script_name:rename_ALL
find ./ -type f | grep “txt” |sed -n ’s//.////p’| nawk ‘{oldname=$1;
sub(//./,”_test/.”);
newname=$1;
print “mv ” oldname ” ” newname }’ | /bin/sh
將上述代碼保存爲文件rename_ALL
修改腳本的屬性,使它能執行:chmod u+x rename_ALL
執行:./rename_ALL
來自網絡
看起來很管用,要是能夠進行將要改的文件名格式傳遞進去就更好了。
3、使用rename命令
Zianed@ubuntu:~/public_web_sSmO9OUVY1/files/image$ rename –help
Unknown option: help
Usage: rename [-v] [-n] [-f] perlexpr [filenames]
man rename發現,
“rename” renames the filenames supplied according to the rule specified as the first argument.
The perlexpr argument is a Perl expression which is expected to modify the $_ string in Perl for at least some of the filenames specified.If a given filename is not modified by the expression,it will not be renamed. If no filenames are given on the command line, filenames will be read via standard input.
For example, to rename all files matching “*.bak” to strip the extension, you might say
rename ’s//.bak$//’ *.bak
To translate uppercase names to lower, you’d use
rename ‘y/A-Z/a-z/’ *
命令格式:
rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]
perlexpr 是一種 Perl 腳本格式的正則表達式。
files 文件名通配符
參數:
-v, –verbose
Verbose: print names of files successfully renamed.
詳細模式:打印成功更改的文件名列表
-n, –no-act
No Action: show what files would have been renamed.
測試模式:並不真正的執行命令,而只是顯示哪些文件名應該怎麼進行更改,用於測試模式。
-f, –force
Force: overwrite existing files.
強制模式:在更改文件名,如果更改後的文件已經存在時覆蓋已經存在的文件。
Zianed@ubuntu:~/public_web_sSmO9OUVY1/files/image$ ls
20090819java1.png 200909v9twitter4.jpg
20090819javaseplatformroadmap.gif 2009vvv0epen_preject.jpg
200908v8lerrn.jpg 2009vvv2jrvr_primitive_drtr_type.jpg
200908v8r.jpg 2009vvv2se_plrtferm.jpg
200909v6neiji.jpg 2009vvv2ser2.jpg
200909v6wriji.jpg 2009vvv2ser3.jpg
200909v9phete.jpg 2009vvv2ser4.jpg
200909v9twitter.jpg 2009vvv2serv.jpg
200909v9twitter2.jpg lege.jpg
200909v9twitter3.jpg
將*.jpg文件名中的09v9改爲0919
Zianed@ubuntu:~/public_web_sSmO9OUVY1/files/image$ rename ’s/09v9/0919/’ *.jpg
Zianed@ubuntu:~/public_web_sSmO9OUVY1/files/image$ ls
20090819java1.png 200909v6wriji.jpg
20090819javaseplatformroadmap.gif 2009vvv0epen_preject.jpg
200908v8lerrn.jpg 2009vvv2jrvr_primitive_drtr_type.jpg
200908v8r.jpg 2009vvv2se_plrtferm.jpg
20090919phete.jpg 2009vvv2ser2.jpg
20090919twitter.jpg 2009vvv2ser3.jpg
20090919twitter2.jpg 2009vvv2ser4.jpg
20090919twitter3.jpg 2009vvv2serv.jpg
20090919twitter4.jpg lege.jpg
200909v6neiji.jpg
rename 的典型應用:
0. 批量更改文件擴展名
$ rename ’s//.txt//.ext/’ *
1. 批量刪除文件擴展名
$ rename ’s//.txt//’ *
2. 批量添加文件擴展名
$ rename ’s/$//.txt/’ *
3. 按自己的方式批量重命名文件
$ rename ’s/(/d)/第$1 章/’ *
$ rename s/_/-/ *.jpg #修改所有的包含_的.jpg文件中的_爲-