1. 程式人生 > >linux 檔案批量操作

linux 檔案批量操作

本篇內容包括以下內容:

  1.      批量建立檔案
  2.      批量改名檔名
  3.      批量刪除檔案

 

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$