我有 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'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句