• Linux学习之批量修改文件名


    1. 通过专业的改名命令rename实现

    [root@oldboy oldboy]# ll
    total 0
    -rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_1_finished.jpg
    -rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_2_finished.jpg
    -rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_3_finished.jpg
    -rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_4_finished.jpg
    -rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_5_finished.jpg
    [root@oldboy oldboy]# rename "finished" ""  * ==>执行批量改名
    [root@oldboy oldboy]# ll
    total 0
    -rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_1_.jpg
    -rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_2_.jpg
    -rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_3_.jpg
    -rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_4_.jpg
    -rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_5_.jpg
    拓展下:改扩展名的方法
    [root@oldboy oldboy]# rename .jpg .oldboy *
    [root@oldboy oldboy]# ll
    total 0
    -rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_1_.oldboy
    -rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_2_.oldboy
    -rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_3_.oldboy
    -rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_4_.oldboy
    -rw-r--r-- 1 root root 0 Nov 13 19:38 stu_102999_5_.oldboy

    2. ls结合awk实现

    [root@oldboy oldboy]# ll
    total 0
    -rw-r--r-- 1 root root 0 Nov 13 18:13 stu_102999_1_finished.jpg
    -rw-r--r-- 1 root root 0 Nov 13 18:13 stu_102999_2_finished.jpg
    -rw-r--r-- 1 root root 0 Nov 13 18:13 stu_102999_3_finished.jpg
    -rw-r--r-- 1 root root 0 Nov 13 18:13 stu_102999_4_finished.jpg
    -rw-r--r-- 1 root root 0 Nov 13 18:13 stu_102999_5_finished.jpg
    [root@oldboy oldboy]# ls |awk -F 'finished' '{print  $0}'
    stu_102999_1_finished.jpg
    stu_102999_2_finished.jpg
    stu_102999_3_finished.jpg
    stu_102999_4_finished.jpg
    stu_102999_5_finished.jpg
    [root@oldboy oldboy]# ls |awk -F 'finished' '{print  $1}'
    stu_102999_1_
    stu_102999_2_
    stu_102999_3_
    stu_102999_4_
    stu_102999_5_
    [root@oldboy oldboy]# ls |awk -F 'finished' '{print  $2}'
    .jpg
    .jpg
    .jpg
    .jpg
    .jpg
    [root@oldboy oldboy]# ls |awk -F 'finished' '{print  $1$2}'
    stu_102999_1_.jpg
    stu_102999_2_.jpg
    stu_102999_3_.jpg
    stu_102999_4_.jpg
    stu_102999_5_.jpg
    [root@oldboy oldboy]# ls |awk -F 'finished' '{print "mv "$0" "$1$2" "}'|/bin/bash
    [root@oldboy oldboy]# ll
    total 0
    -rw-r--r-- 1 root root 0 Nov 13 18:13 stu_102999_1_.jpg
    -rw-r--r-- 1 root root 0 Nov 13 18:13 stu_102999_2_.jpg
    -rw-r--r-- 1 root root 0 Nov 13 18:13 stu_102999_3_.jpg
    -rw-r--r-- 1 root root 0 Nov 13 18:13 stu_102999_4_.jpg
    -rw-r--r-- 1 root root 0 Nov 13 18:13 stu_102999_5_.jpg

    3. shell脚本for循环加变量的部分截取方法

    [root@oldboy oldboy]#ls -l
    total 4
    -rw-r--r-- 1 root root 114 Nov 14 00:21 change_file_name.sh
    -rw-r--r-- 1 root root   0 Nov 14 00:23 stu_102999_1_finished.jpg
    -rw-r--r-- 1 root root   0 Nov 14 00:23 stu_102999_2_finished.jpg
    -rw-r--r-- 1 root root   0 Nov 14 00:23 stu_102999_3_finished.jpg
    -rw-r--r-- 1 root root   0 Nov 14 00:23 stu_102999_4_finished.jpg
    -rw-r--r-- 1 root root   0 Nov 14 00:23 stu_102999_5_finished.jpg
    [root@oldboy oldboy]#cat change_file_name.sh 
    #!/bin/sh
    # oldboy QQ:31333741
    for file in `ls ./*.jpg` 
     do 
    /bin/mv $file `echo "${file%finished*}.jpg"`  #这里就是变量的截取新方法,在老男孩的shell编程课程中会讲到此用法。
    done 
    [root@oldboy oldboy]#sh change_file_name.sh 
    [root@oldboy oldboy]#ls -l
    total 4
    -rw-r--r-- 1 root root 114 Nov 14 00:21 change_file_name.sh
    -rw-r--r-- 1 root root   0 Nov 14 00:23 stu_102999_1_.jpg
    -rw-r--r-- 1 root root   0 Nov 14 00:23 stu_102999_2_.jpg
    -rw-r--r-- 1 root root   0 Nov 14 00:23 stu_102999_3_.jpg
    -rw-r--r-- 1 root root   0 Nov 14 00:23 stu_102999_4_.jpg
    -rw-r--r-- 1 root root   0 Nov 14 00:23 stu_102999_5_.jpg
    特别提示:本脚本和文件在同一目录

    4. shell脚本for循环结合sed实现

    [检查数据] 
    [root@oldboy oldboy]# ll
    total 4
    -rw-r--r-- 1 root root 85 Oct  2 12:54 oldboy_modi_file.sh
    -rw-r--r-- 1 root root  0 Oct  2 12:50 stu_102999_1_finished.jpg
    -rw-r--r-- 1 root root  0 Oct  2 12:50 stu_102999_2_finished.jpg
    -rw-r--r-- 1 root root  0 Oct  2 12:50 stu_102999_3_finished.jpg
    -rw-r--r-- 1 root root  0 Oct  2 12:50 stu_102999_4_finished.jpg
    -rw-r--r-- 1 root root  0 Oct  2 12:50 stu_102999_5_finished.jpg
    
    [开发脚本]
    [root@oldboy oldboy]# cat oldboy_modi_file.sh 
    #!/bin/sh
    for file in `ls ./*.jpg`  
    #shell脚本for循环,file为变量依次取得ls ./*.jpg的结果文件名
    do
    mv $file `echo $file|sed 's/finished//g'` 
    #使用mv命令进行更改文件,新的文件名字符串拼接是本题的重点。
    done
    #特别提示:通过本题,可以学习到for循环的使用,及mv、sed等命令的使用
    
    [执行脚本]
    [root@oldboy oldboy]# sh oldboy_modi_file.sh 
    
    [检查结果]
    [root@oldboy oldboy]# ll
    total 4
    -rw-r--r-- 1 root root 85 Oct  2 12:54 oldboy_modi_file.sh
    -rw-r--r-- 1 root root  0 Oct  2 12:50 stu_102999_1_.jpg
    -rw-r--r-- 1 root root  0 Oct  2 12:50 stu_102999_2_.jpg
    -rw-r--r-- 1 root root  0 Oct  2 12:50 stu_102999_3_.jpg
    -rw-r--r-- 1 root root  0 Oct  2 12:50 stu_102999_4_.jpg
    -rw-r--r-- 1 root root  0 Oct  2 12:50 stu_102999_5_.jpg

    转自老男孩博客:http://oldboy.blog.51cto.com/2561410/711342

  • 相关阅读:
    Remove Element
    Binary Tree Inorder Traversal
    Symmetric Tree
    Roman to Integer
    Search Insert Position
    Reverse Integer
    Pascal's Triangle
    Merge Sorted Array
    Same Tree
    Visual Studio Code 做PHP开发
  • 原文地址:https://www.cnblogs.com/sunfie/p/6742641.html
Copyright © 2020-2023  润新知