日期:2014-05-16  浏览次数:20711 次

Linux 批量更改文件名命令

想把一部分文件加上日期序。也就是在文件名称前加上当前的年月日进行输出。
获取日期格式如下
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????????????????