如果文件内容匹配,则重命名大量文件夹 [英] Rename bulk of folder if matching content of a file

查看:72
本文介绍了如果文件内容匹配,则重命名大量文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有6000个文件夹.例子:

I have 6000 folders. Examples :

Niceman Production
Jingle Production
Watch Production

我有一个包含以下内容的文件:

I have a file containing :

Title: Watch Production Code: SL-990
Title: Jingle Production Code: UOP-222
Title: Niceman Production Code: KP-290

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

What I need to do is to rename the folders above if their name matches a line from this file. The new name must be this:

Niceman Production KP-290
Watch Production SL-990
Jingle Production UOP-222

在bash脚本编写中有可能做到这一点吗?

Is it possible in bash scripting to do that ?

推荐答案

这是我的看法.

#!/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 .
    • Same thing as what @Oguz ismail did, remove the echo if you think the output is correct.
    • 试运行.

      mkdir -p /tmp/testing123 && cd /tmp/testing123
      

      mkdir -p 'Niceman Production'
      mkdir -p 'Jingle Production'
      mkdir -p 'Watch Production'
      

      确保 script 和文件位于同一目录内.

      Make sure the script and the file is inside the same directory.

      bash ./myscript
      

      输出

      renamed './Watch Production' -> './Watch Production SL-990'
      renamed './Niceman Production' -> './Niceman Production KP-290'
      renamed './Jingle Production' -> './Jingle Production UOP-222'
      

      这篇关于如果文件内容匹配,则重命名大量文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆