Ubuntu批量修改文件名

文章目录


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

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

bash 复制代码
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/")"' {} ; 解析

bash 复制代码
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 命令将文件重命名为新的名称。

参考


相关推荐
大树8811 小时前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠11 小时前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质11 小时前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
bush411 小时前
嵌入式linux学习记录十四、术语
linux·嵌入式
载数而行52011 小时前
Linux 11 动态监控指令top
linux
Inhand陈工12 小时前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智12 小时前
ARP代理--工作原理
运维·网络·arp·arp代理
不会C语言的男孩13 小时前
Linux 系统编程 · 第 8 章:进程基础
linux·c语言
shushangyun_13 小时前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
古城小栈13 小时前
Unix 与 Linux 异同小叙
linux·服务器·unix