首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果与文件内容匹配,则重命名大容量文件夹

如果与文件内容匹配,则重命名大容量文件夹
EN

Stack Overflow用户
提问于 2020-04-20 06:33:10
回答 1查看 59关注 0票数 0

我有6000个文件夹。例子:

代码语言:javascript
复制
Niceman Production
Jingle Production
Watch Production

我有一份文件,里面有:

代码语言:javascript
复制
Title: Watch Production Code: SL-990
Title: Jingle Production Code: UOP-222
Title: Niceman Production Code: KP-290

我需要做的是重命名上面的文件夹,如果它们的名称与此文件中的一行相匹配。新的名称必须是:

代码语言:javascript
复制
Niceman Production KP-290
Watch Production SL-990
Jingle Production UOP-222

在bash脚本中可以做到这一点吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-20 07:34:01

这是我对此的看法。

代码语言:javascript
复制
#!/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

一次测试。

代码语言:javascript
复制
mkdir -p /tmp/testing123 && cd /tmp/testing123
代码语言:javascript
复制
mkdir -p 'Niceman Production'
mkdir -p 'Jingle Production'
mkdir -p 'Watch Production'

确保script和文件位于同一个目录中。

代码语言:javascript
复制
bash ./myscript

输出

代码语言:javascript
复制
renamed './Watch Production' -> './Watch Production SL-990'
renamed './Niceman Production' -> './Niceman Production KP-290'
renamed './Jingle Production' -> './Jingle Production UOP-222'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61316385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档