我有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脚本中可以做到这一点吗?
发布于 2020-04-20 07:34:01
这是我对此的看法。
#!/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/testing123mkdir -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'https://stackoverflow.com/questions/61316385
复制相似问题