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

参考


相关推荐
律宏阔3 小时前
WSL Docker 端口明明空闲,但就是绑不上端口
linux·windows
律宏阔3 小时前
WSL 突然断网,无法 ping 内网或外网
linux·windows
穷人小水滴3 小时前
用容器编译 VirtualBox 虚拟机软件 (ArchLinux, podman)
linux·容器·virtualbox
乱码三千3 小时前
如何优雅地直连无公网 IP 的远程 GPU 服务器
linux·人工智能·程序员
yunwei373 小时前
使用 eBPF 跟踪 Nginx 请求
linux·后端·性能优化
yunwei373 小时前
使用 eBPF 跟踪 MySQL 查询
linux·后端·性能优化
yunwei373 小时前
eBPF 示例教程:使用 XDP 捕获 TCP 信息
linux·后端·性能优化
judezh3 小时前
api 容器一直 unhealthy?我把一个 Agent 运行时的健康检查逐条拆了,查出四个问题
运维·docker
GeW3 小时前
制造业高端化,为什么必须重估Linux和数据库?
linux
GeW3 小时前
工业场景中的Linux与数据库选型:高端化转型的技术底座
linux