且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-22 08:19:58

这是我的看法.

#!/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'