Linux:批量修改文件名称的脚本
如题所示,本文将介绍如何在Linux中批量修改某个路径下的文件名称为有序序列。
先看看效果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
[root@ora11g Pictural]# ll total 16340 -rw-r--r-- 1 root root 3135741 Oct 9 08:57 1.gif -rw-r--r-- 1 root root 3135741 Oct 9 08:57 5366d0160924ab183d83e08334fae6cd7a890be8.gif -rw-r--r-- 1 root root 2766824 Oct 9 08:57 5687791ed21b0ef4c655ac85dcc451da80cb3e75.jpg -rw-r--r-- 1 root root 2303872 Oct 9 08:57 5eea2adda3cc7cd9081fff033801213fb90e9184.jpg -rw-r--r-- 1 root root 2383037 Oct 9 08:57 70cea21ea8d3fd1ffa9e95e1314e251f94ca5f1f.jpg -rw-r--r-- 1 root root 2973218 Oct 9 08:57 9095f303918fa0ec3fc553d6279759ee3c6ddbb4.jpg [root@ora11g Pictural]# [root@ora11g Pictural]# sh /script/change_file_name_in_sequence.sh ---------------------------- Change File Name Start: Thu Oct 9 09:54:15 CST 2014 ---------------------------- ========================================== ### Orig: /software/Pictural/1.gif ### New: /software/Pictural/1.jpg ### Done. ========================================== ### Orig: /software/Pictural/5366d0160924ab183d83e08334fae6cd7a890be8.gif ### New: /software/Pictural/2.jpg ### Done. ========================================== ### Orig: /software/Pictural/5687791ed21b0ef4c655ac85dcc451da80cb3e75.jpg ### New: /software/Pictural/3.jpg ### Done. ========================================== ### Orig: /software/Pictural/5eea2adda3cc7cd9081fff033801213fb90e9184.jpg ### New: /software/Pictural/4.jpg ### Done. ========================================== ### Orig: /software/Pictural/70cea21ea8d3fd1ffa9e95e1314e251f94ca5f1f.jpg ### New: /software/Pictural/5.jpg ### Done. ========================================== ### Orig: /software/Pictural/9095f303918fa0ec3fc553d6279759ee3c6ddbb4.jpg ### New: /software/Pictural/6.jpg ### Done. ---------------------------- End: Thu Oct 9 09:54:15 CST 2014 ---------------------------- [root@ora11g Pictural]# [root@ora11g Pictural]# ll total 16340 -rw-r--r-- 1 root root 3135741 Oct 9 08:57 1.jpg -rw-r--r-- 1 root root 3135741 Oct 9 08:57 2.jpg -rw-r--r-- 1 root root 2766824 Oct 9 08:57 3.jpg -rw-r--r-- 1 root root 2303872 Oct 9 08:57 4.jpg -rw-r--r-- 1 root root 2383037 Oct 9 08:57 5.jpg -rw-r--r-- 1 root root 2973218 Oct 9 08:57 6.jpg [root@ora11g Pictural]# |
脚本的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
[root@ora11g Pictural]# cat /script/change_file_name_in_sequence.sh echo "----------------------------" echo "Change File Name" echo "Start: "`date` echo "----------------------------" count=1 file_list=`ls` for file_item in $file_list do orig_path=`pwd $file_item`"/$file_item" new_path=`pwd $file_item`"/"$count".jpg" echo "==========================================" echo "### Orig: "$orig_path echo "### New: "$new_path mv $orig_path $new_path; echo "### Done." count=$(($count+1)) done echo "----------------------------" echo "End: "`date` echo "----------------------------" [root@ora11g Pictural]# |
————————————————————————————————————————————
Ending。