linux文件批量操作系统,linux下批量修改文件名的详细操作方法及示例

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 %fn|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 %fn|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文件中的_为-

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值