zoukankan      html  css  js  c++  java
  • 一行Linux命令批量修改文件名

    前言

      最好的方法不一定是你最快能想到的。工作中针对临时使用的脚本不要求健壮,写出来越快越好。这里提供一种使用sed命令构造命令解决处理批量文件的技巧,供参考。

    需求案例1

      将当前目录下所有的0_80_91.txt、0_80_92.txt、0_80_93.txt、。。。等几十个文件的文件名修改为0_81_91.txt、0_81_92.txt、0_81_93.txt。也就是将文件名中的80修改为81。

      实现命令为:ls *.txt |sed -nr 's/(0_)(80)(.*)/mv 123 1813/gp' | sh

    #ls *.txt 
    0_80_91.txt  0_80_92.txt  0_80_93.txt
    #ls *.txt |sed -nr 's/(0_)(80)(.*)/mv 123 1813/gp'
    mv 0_80_91.txt 0_81_91.txt
    mv 0_80_92.txt 0_81_92.txt
    mv 0_80_93.txt 0_81_93.txt
    #ls *.txt |sed -nr 's/(0_)(80)(.*)/mv 123 1813/gp' | sh
    #ls *.txt
    0_81_91.txt  0_81_92.txt  0_81_93.txt

    需求案例2

      将当前目录下的所有的0_80_91.Z、0_80_92.Z、0_80_93.Z文件通过命令cc_uncompress调用解压并输出到指定文件。调用格式为cc_uncompress -s 0_80_91.txt -d 1.txt。1.txt可以是任意文件名。

      实现命令1为:ls *.Z | sed -nr 's/(.*)/cc_uncompress -s 1 -d 1.txt/gp'

    #ls *.Z | sed -nr 's/(.*)/cc_uncompress -s 1 -d 1.txt/gp'
    cc_uncompress -s 0_80_91.Z -d 0_80_91.Z.txt
    cc_uncompress -s 0_80_92.Z -d 0_80_92.Z.txt
    cc_uncompress -s 0_80_93.Z -d 0_80_93.Z.txt
    #ls *.Z | sed -nr 's/(.*)/cc_uncompress -s 1 -d 1.txt/gp' | sh

     实现命令2为:find . -name "*.Z" -exec cc_uncompress -s {} -d {}.bak ;

  • 相关阅读:
    java矩阵运算包ujmp中的一些小示例和注意事项
    CSS文字段落排版常用设置
    HTML中标签元素的分类
    三种CSS样式-内联、嵌入、外部
    MySQL常用命令
    解决谷歌浏览器在win8下没有注册类的问题
    转:jQuery.lazyload详解使用方法
    php取整
    限制表单Input的长度,当达到一定长度时不能再输入
    滑动后定位
  • 原文地址:https://www.cnblogs.com/linyfeng/p/10198832.html
Copyright © 2011-2022 走看看