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

参考


相关推荐
bohu837 分钟前
亚博microros小车-原生ubuntu支持系列:16 机器人状态估计
ubuntu·机器人·imu·localization·microros·imu_tools
马立杰1 小时前
H3CNE-33-BGP
运维·网络·h3cne
云空2 小时前
《DeepSeek 网页/API 性能异常(DeepSeek Web/API Degraded Performance):网络安全日志》
运维·人工智能·web安全·网络安全·开源·网络攻击模型·安全威胁分析
深度Linux2 小时前
Linux网络编程中的零拷贝:提升性能的秘密武器
linux·linux内核·零拷贝技术
没有名字的小羊3 小时前
Cyber Security 101-Build Your Cyber Security Career-Security Principles(安全原则)
运维·网络·安全
千夜啊3 小时前
Nginx 运维开发高频面试题详解
运维·nginx·运维开发
存储服务专家StorageExpert4 小时前
答疑解惑:如何监控EMC unity存储系统磁盘重构rebuild进度
运维·unity·存储维护·emc存储
chian-ocean6 小时前
从理论到实践:Linux 进程替换与 exec 系列函数
linux·运维·服务器
拎得清n6 小时前
UDP编程
linux
敖行客 Allthinker6 小时前
从 UTC 日期时间字符串获取 Unix 时间戳:C 和 C++ 中的挑战与解决方案
linux·运维·服务器·c++