Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5504949
  • 博文数量: 922
  • 博客积分: 19333
  • 博客等级: 上将
  • 技术积分: 11226
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-27 14:33
文章分类

全部博文(922)

文章存档

2023年(1)

2020年(2)

2019年(1)

2017年(1)

2016年(3)

2015年(10)

2014年(17)

2013年(49)

2012年(291)

2011年(266)

2010年(95)

2009年(54)

2008年(132)

分类: LINUX

2012-01-31 08:29:41

批量重命名文件的技巧
尝试了半天,终于找到了几种批量重命名文件的方法,记录下来以备后用^_^

[quietheart@lv-k temp]$ touch {1,2,3,4,5}.cpp
[quietheart@lv-k temp]$ touch [1-5]
[quietheart@lv-k temp]$ ls
[1-5]  1.cpp  2.cpp  3.cpp  4.cpp  5.cpp
[quietheart@lv-k temp]$ rm \[1-5\]
rm:是否删除 一般空文件 “[1-5]”? y
[quietheart@lv-k temp]$ ls
1.cpp  2.cpp  3.cpp  4.cpp  5.cpp
[quietheart@lv-k temp]$ rename .cpp .c *.cpp
[quietheart@lv-k temp]$ ls
1.c  2.c  3.c  4.c  5.c
[quietheart@lv-k temp]$ ls *.c |awk '{sub(/.c$/,"")}{print $1}'|awk '{printf "mv %s.c %s.cpp\n",$1,$1}' |sh
[quietheart@lv-k temp]$ ls
1.cpp  2.cpp  3.cpp  4.cpp  5.cpp
[quietheart@lv-k temp]$ ls *.cpp |sed 's/\(.*\).cpp/mv & \1.c/' |sh
[quietheart@lv-k temp]$ ls
1.c  2.c  3.c  4.c  5.c

以上过程大致是:
先把所有".cpp"文件的后缀修改成".c"后缀,再把".c"修改回".cpp",再把".cpp"修改成".c"。
原理分别用到了:使用rename、使用awk+sh、使用sed+正则+sh,三种方式。
阅读(1359) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~