Ubuntu批量修改文件名


批量重命名文件:Ubuntu下使用find命令结合sed和mv

在日常开发和文件管理中,有时我们需要批量重命名一批文件,比如将文件名中的某个特定字符串替换为另一个字符串。在这个案例中,我们要将文件名中包含 " - 副本.h" 的部分替换为 “_ori.h”。

include$ ls
'Converter - 副本.h'    'KeyFrame - 副本.h'          'Map - 副本.h'        'ORBextractor - 副本.h'   'Sim3Solver - 副本.h'
 Converter.h            'KeyFrameDatabase - 副本.h'  'MapDrawer - 副本.h'   ORBextractor.h            Sim3Solver.h
'Frame - 副本.h'         KeyFrameDatabase.h           MapDrawer.h          'ORBmatcher - 副本.h'     'System - 副本.h'
'FrameDrawer - 副本.h'   KeyFrame.h                   Map.h                 ORBmatcher.h              System.h
 FrameDrawer.h          'LocalMapping - 副本.h'      'MapPoint - 副本.h'   'ORBVocabulary - 副本.h'  'Tracking - 副本.h'
 Frame.h                 LocalMapping.h               MapPoint.h            ORBVocabulary.h           Tracking.h
'Initializer - 副本.h'  'LoopClosing - 副本.h'       'Optimizer - 副本.h'  'PnPsolver - 副本.h'      'Viewer - 副本.h'
 Initializer.h           LoopClosing.h                Optimizer.h           PnPsolver.h               Viewer.h

find . -type f -name “* - 副本.h” -exec bash -c ‘mv “ 0 " " 0" " 0""(echo “$0” | sed “s/ - 副本.h/_ori.h/”)”’ {} ; 解析

include$ ls
Converter.h        Initializer.h           LocalMapping.h      Map.h            ORBextractor.h       PnPsolver.h       Tracking.h
Converter_ori.h    Initializer_ori.h       LocalMapping_ori.h  Map_ori.h        ORBextractor_ori.h   PnPsolver_ori.h   Tracking_ori.h
FrameDrawer.h      KeyFrameDatabase.h      LoopClosing.h       MapPoint.h       ORBmatcher.h         Sim3Solver.h      Viewer.h
FrameDrawer_ori.h  KeyFrameDatabase_ori.h  LoopClosing_ori.h   MapPoint_ori.h   ORBmatcher_ori.h     Sim3Solver_ori.h  Viewer_ori.h
Frame.h            KeyFrame.h              MapDrawer.h         Optimizer.h      ORBVocabulary.h      System.h
Frame_ori.h        KeyFrame_ori.h          MapDrawer_ori.h     Optimizer_ori.h  ORBVocabulary_ori.h  System_ori.h

find . -type f -name “* - 副本.h” -exec bash -c ‘mv “ 0 " " 0" " 0""(echo “$0” | sed “s/ - 副本.h/_ori.h/”)”’ {} ;

  • find .:
    • 从当前目录开始查找文件。
  • type f:
    • 指定查找的类型为文件(排除目录)。
  • name “* - 副本.h”:
    • 匹配所有以 " - 副本.h" 结尾的文件名。
  • exec bash -c ‘…’ {} ;:
    • 对每个匹配的文件执行指定的操作。
  • echo “$0” | sed “s/ - 副本.h/_ori.h/”:
    • 使用 sed 将文件名中的 " - 副本.h" 替换为 “_ori.h”。
    • s/ - 副本.h/_ori.h/ 是 sed 替换命令的语法。
  • mv “ 0 " " 0" " 0""(echo …)”:
    • 使用 mv 命令将文件重命名为新的名称。

参考


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

草莓奶忻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值