You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写Bash脚本通过正则匹配按不同规则批量重命名文件夹?

批量重命名文件夹脚本修正方案

以下是无需分类即可批量处理的Bash脚本,基于Perl版rename命令实现(需提前安装,Debian/Ubuntu可通过apt install rename安装,Fedora则是dnf install perl-rename):

#!/bin/bash

# 先执行 dry run 预览修改效果(确认无误后移除所有 rename 命令中的 -n 参数)
# 处理「品牌 → 日期 → 名称 → and ...」格式(自动首字母大写and部分)
rename -n 's/^(Universal|Sony) (\d{4} \d{2} \d{2}) (.*?) (and .+)$/$3 - $2 - \u$4 - $1/' */

# 处理「品牌 → 日期 → 名称」格式
rename -n 's/^(Universal|Sony) (\d{4} \d{2} \d{2}) (.+)$/$3 - $2 - $1/' */

# 处理「品牌 → 名称 → 日期 → and ...」格式(保留and小写)
rename -n 's/^(Universal|Sony) (.+?) (\d{4} \d{2} \d{2}) (and .+)$/$2 - $3 - $4 - $1/' */

# 处理「品牌 → 名称 → 日期」格式
rename -n 's/^(Universal|Sony) (.+?) (\d{4} \d{2} \d{2})$/$2 - $3 - $1/' */

脚本说明

  1. 采用分批次匹配不同格式的方式,避免复杂条件分支,解决了你原脚本的语法错误问题。
  2. 使用-n参数预演修改效果,不会实际修改文件名,确认输出符合预期后再移除该参数执行真实重命名。
  3. 自动适配两种品牌位置(日期前/后)以及是否包含and后缀的场景,其中日期后紧跟and的场景会自动首字母大写,完全匹配你给出的示例格式要求。

使用方法:将脚本保存为rename_folders.sh,赋予执行权限(chmod +x rename_folders.sh),在包含目标文件夹的目录下运行即可。

内容的提问来源于stack exchange,提问作者Martin Julien

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 03:32:35