linux - 如果文件的内容匹配,则重命名大量文件夹
问题描述
我有 6000 个文件夹。例子 :
Niceman Production
Jingle Production
Watch Production
我有一个文件包含:
Title: Watch Production Code: SL-990
Title: Jingle Production Code: UOP-222
Title: Niceman Production Code: KP-290
如果它们的名称与该文件中的一行匹配,我需要做的是重命名上面的文件夹。新名称必须是这样的:
Niceman Production KP-290
Watch Production SL-990
Jingle Production UOP-222
在 bash 脚本中可以做到这一点吗?
解决方案
这是我的看法。
#!/usr/bin/env bash
##: Save the contents of the file in an array name contents
mapfile -t contents < file.txt
##: Loop through the directories
while IFS= read -r -d '' directories; do
for i in "${contents[@]}"; do ##: Loop through the file contents
if [[ $i = *${directories#*./}* ]]; then ##: If they both match
echo mv -v "$directories" "$directories ${i##* }" ##: Rename to the desired output.
fi
done
done < <(find . ! -name . -type d -print0) ##: Look for directories using find.
- 与所做的相同,如果您认为输出正确
@Oguz ismail
,请删除。echo
试运行。
mkdir -p /tmp/testing123 && cd /tmp/testing123
mkdir -p 'Niceman Production'
mkdir -p 'Jingle Production'
mkdir -p 'Watch Production'
确保script
和文件在同一目录中。
bash ./myscript
输出
renamed './Watch Production' -> './Watch Production SL-990'
renamed './Niceman Production' -> './Niceman Production KP-290'
renamed './Jingle Production' -> './Jingle Production UOP-222'
推荐阅读
- python - 例外:未找到 Jupyter 命令“jupyter-notebook”,windows
- css - 媒体查询和 Rails 资产管道
- wordpress - 如何从 WordPress 链接中删除硬编码的域名?
- java - 如何为我的 Java 纸牌游戏引用数组中的索引位置
- windows - kubectl config set-credentials --auth-provider:错误:未知标志:--auth-provider
- python - 由于文件不存在而将更改推送到 GitHub 的问题
- javascript - 使用 Vanilla Javascript 模拟 TextArea 上的按键(带演示)
- javascript - HTML 无法识别事件监听器
- php - Laravel 队列 - 缓存中的数据库?
- php - 通过数组引用进行循环检测