linux 文件批量操作
本篇内容包括以下内容:
- 批量创建文件
- 批量改名文件名
- 批量删除文件
1.批量创建文件
这里说下就目前我所掌握的两种方法:
1.使用seq 法:
[email protected]:~/Code/Shell$ ls
batch.sh
[email protected]:~/Code/Shell$ for i in `seq 1 10` ; do touch file_${i}_finished.jpg; done
[email protected]:~/Code/Shell$ ls
batch.sh file_1_finished.jpg file_3_finished.jpg file_5_finished.jpg file_7_finished.jpg file_9_finished.jpg
file_10_finished.jpg file_2_finished.jpg file_4_finished.jpg file_6_finished.jpg file_8_finished.jpg
[email protected]:~/Code/Shell$
2.通配符法:
[email protected]:~/Code/Shell$ touch file_{1..10}_finished.html
[email protected]:~/Code/Shell$ ls
batch.sh file_1_finished.jpg file_3_finished.jpg file_5_finished.jpg file_7_finished.jpg file_9_finished.jpg
file_10_finished.html file_2_finished.html file_4_finished.html file_6_finished.html file_8_finished.html
file_10_finished.jpg file_2_finished.jpg file_4_finished.jpg file_6_finished.jpg file_8_finished.jpg
file_1_finished.html file_3_finished.html file_5_finished.html file_7_finished.html file_9_finished.html
[email protected]:~/Code/Shell$
以上方法可以在shell 中去实现。
2.批量修改文件
1.使用mv 命令
将该目录下,所有.jpg 文件改名为.c 文件
[email protected]:~/Code/Shell$ for fileName in `ls *.jpg` ; do mv $fileName ${fileName%.jpg}.c ;done
[email protected]:~/Code/Shell$ ls
batch.sh file_1_finished.html file_3_finished.html file_5_finished.html file_7_finished.html file_9_finished.html
file_10_finished.c file_2_finished.c file_4_finished.c file_6_finished.c file_8_finished.c
file_10_finished.html file_2_finished.html file_4_finished.html file_6_finished.html file_8_finished.html
file_1_finished.c file_3_finished.c file_5_finished.c file_7_finished.c file_9_finished.c
[email protected]:~/Code/Shell$
2.使用rename 方式实现
将所有.c 文件的finished 去掉
这里有必要说下,网上说的 rename "finished" "" .c 是行不通的,这个rename 命令是在不同发行版是不同的,建议使用前 man rename 下。
我这里发行版本是ubuntu 14.04 ,下面给出man rename 结果。
ok,下面实现改将所有.c 文件的finished 去掉:
[email protected]:~/Code/Shell$ ls
batch.sh file_1_finished.html file_3_finished.html file_5_finished.html file_7_finished.html file_9_finished.html
file_10_finished.c file_2_finished.c file_4_finished.c file_6_finished.c file_8_finished.c
file_10_finished.html file_2_finished.html file_4_finished.html file_6_finished.html file_8_finished.html
file_1_finished.c file_3_finished.c file_5_finished.c file_7_finished.c file_9_finished.c
[email protected]:~/Code/Shell$ rename 's/finished//' *.c
[email protected]:~/Code/Shell$ ls
batch.sh file_1_finished.html file_3_finished.html file_5_finished.html file_7_finished.html file_9_finished.html
file_10_.c file_2_.c file_4_.c file_6_.c file_8_.c
file_10_finished.html file_2_finished.html file_4_finished.html file_6_finished.html file_8_finished.html
file_1_.c file_3_.c file_5_.c file_7_.c file_9_.c
[email protected]:~/Code/Shell$
3. ls + awk 结合
这个方法借鉴于老男孩博客:http://blog.51cto.com/oldboy/711342
去除 所有.html 文件finished
[email protected]:~/Code/Shell$ ls
batch.sh file_1_.c file_2_finished.html file_4_.c file_5_finished.html file_7_.c file_8_finished.html
file_10_.c file_1_finished.html file_3_.c file_4_finished.html file_6_.c file_7_finished.html file_9_.c
file_10_finished.html file_2_.c file_3_finished.html file_5_.c file_6_finished.html file_8_.c file_9_finished.html
[email protected]:~/Code/Shell$ ls *.html | awk -F 'finished' '{print $0}'
file_10_finished.html
file_1_finished.html
file_2_finished.html
file_3_finished.html
file_4_finished.html
file_5_finished.html
file_6_finished.html
file_7_finished.html
file_8_finished.html
file_9_finished.html
[email protected]:~/Code/Shell$ ls *.html | awk -F 'finished' '{print $1}'
file_10_
file_1_
file_2_
file_3_
file_4_
file_5_
file_6_
file_7_
file_8_
file_9_
[email protected]:~/Code/Shell$ ls *.html | awk -F 'finished' '{print $2}'
.html
.html
.html
.html
.html
.html
.html
.html
.html
.html
[email protected]:~/Code/Shell$ ls *.html | awk -F 'finished' '{print $1$2}'
file_10_.html
file_1_.html
file_2_.html
file_3_.html
file_4_.html
file_5_.html
file_6_.html
file_7_.html
file_8_.html
file_9_.html
[email protected]:~/Code/Shell$ ls *.html | awk -F 'finished' '{print "mv "$0" "$1$2" "}' | /bin/bash
[email protected]:~/Code/Shell$ ls
batch.sh file_10_.html file_1_.html file_2_.html file_3_.html file_4_.html file_5_.html file_6_.html file_7_.html file_8_.html file_9_.html
file_10_.c file_1_.c file_2_.c file_3_.c file_4_.c file_5_.c file_6_.c file_7_.c file_8_.c file_9_.c
[email protected]:~/Code/Shell$
4. mv + sed 命令
这个方法借鉴于老男孩博客:http://blog.51cto.com/oldboy/711342
去除 所有.swf 文件finished
[email protected]:~/Code/Shell$ touch today_{1..10}_finished.swf
[email protected]:~/Code/Shell$ ls
batch.sh file_1_.html file_3_.html file_5_.html file_7_.html file_9_.html today_3_finished.swf today_7_finished.swf
file_10_.c file_2_.c file_4_.c file_6_.c file_8_.c today_10_finished.swf today_4_finished.swf today_8_finished.swf
file_10_.html file_2_.html file_4_.html file_6_.html file_8_.html today_1_finished.swf today_5_finished.swf today_9_finished.swf
file_1_.c file_3_.c file_5_.c file_7_.c file_9_.c today_2_finished.swf today_6_finished.swf
[email protected]:~/Code/Shell$ for fileName in `ls *.swf` ;do mv $fileName `echo $fileName | sed 's/finished//g'` ;done
[email protected]:~/Code/Shell$ ls
batch.sh file_1_.c file_2_.html file_4_.c file_5_.html file_7_.c file_8_.html today_10_.swf today_3_.swf today_6_.swf today_9_.swf
file_10_.c file_1_.html file_3_.c file_4_.html file_6_.c file_7_.html file_9_.c today_1_.swf today_4_.swf today_7_.swf
file_10_.html file_2_.c file_3_.html file_5_.c file_6_.html file_8_.c file_9_.html today_2_.swf today_5_.swf today_8_.swf
[email protected]:~/Code/Shell$
3.批量删除文件
批量删除文件就简单的多,一般使用通配符:
删除所有swf 结尾的文件
[email protected]:~/Code/Shell$ ls
batch.sh file_1_.c file_2_.html file_4_.c file_5_.html file_7_.c file_8_.html today_10_.swf today_3_.swf today_6_.swf today_9_.swf
file_10_.c file_1_.html file_3_.c file_4_.html file_6_.c file_7_.html file_9_.c today_1_.swf today_4_.swf today_7_.swf
file_10_.html file_2_.c file_3_.html file_5_.c file_6_.html file_8_.c file_9_.html today_2_.swf today_5_.swf today_8_.swf
[email protected]:~/Code/Shell$ rm -rf *.swf
[email protected]:~/Code/Shell$ ls
batch.sh file_10_.html file_1_.html file_2_.html file_3_.html file_4_.html file_5_.html file_6_.html file_7_.html file_8_.html file_9_.html
file_10_.c file_1_.c file_2_.c file_3_.c file_4_.c file_5_.c file_6_.c file_7_.c file_8_.c file_9_.c
[email protected]:~/Code/Shell$
下面再给出一个例子:
[email protected]:~/Code/Shell$ touch {1..20}_stu.jpg
[email protected]:~/Code/Shell$ ls
10_stu.jpg 12_stu.jpg 14_stu.jpg 16_stu.jpg 18_stu.jpg 1_stu.jpg 2_stu.jpg 4_stu.jpg 6_stu.jpg 8_stu.jpg batch.sh
11_stu.jpg 13_stu.jpg 15_stu.jpg 17_stu.jpg 19_stu.jpg 20_stu.jpg 3_stu.jpg 5_stu.jpg 7_stu.jpg 9_stu.jpg
[email protected]:~/Code/Shell$ touch {1..20}_teacher.jpg
[email protected]:~/Code/Shell$ ls
10_stu.jpg 12_stu.jpg 14_stu.jpg 16_stu.jpg 18_stu.jpg 1_stu.jpg 2_stu.jpg 4_stu.jpg 6_stu.jpg 8_stu.jpg batch.sh
10_teacher.jpg 12_teacher.jpg 14_teacher.jpg 16_teacher.jpg 18_teacher.jpg 1_teacher.jpg 2_teacher.jpg 4_teacher.jpg 6_teacher.jpg 8_teacher.jpg
11_stu.jpg 13_stu.jpg 15_stu.jpg 17_stu.jpg 19_stu.jpg 20_stu.jpg 3_stu.jpg 5_stu.jpg 7_stu.jpg 9_stu.jpg
11_teacher.jpg 13_teacher.jpg 15_teacher.jpg 17_teacher.jpg 19_teacher.jpg 20_teacher.jpg 3_teacher.jpg 5_teacher.jpg 7_teacher.jpg 9_teacher.jpg
[email protected]:~/Code/Shell$ rm -rf {1..20}_stu.jpg
[email protected]:~/Code/Shell$ ls
10_teacher.jpg 12_teacher.jpg 14_teacher.jpg 16_teacher.jpg 18_teacher.jpg 1_teacher.jpg 2_teacher.jpg 4_teacher.jpg 6_teacher.jpg 8_teacher.jpg batch.sh
11_teacher.jpg 13_teacher.jpg 15_teacher.jpg 17_teacher.jpg 19_teacher.jpg 20_teacher.jpg 3_teacher.jpg 5_teacher.jpg 7_teacher.jpg 9_teacher.jpg
[email protected]:~/Code/Shell$