我需要一个帮助才能完成重命名文件夹的脚本。
例如:我当前的文件夹和文件如下:
Gideon/gideon_lisha/Gideon_samuel/Gideon_nathan.xml
Gideon/lisha_gideon/Gideon_noah.xml
...
我想要一个shell命令来重命名它们,如下所示:
Liang/Liang_lisha/Liang_samuel/Liang_nathan.xml
Liang/lisha_Liang/Liang_noah.xml
...
我并列:
#!/bin/bash
path=$1
filename=$2
newfilename=$3
echo "We are finding '$filename' under the folder '$path'"
count=1
for i in `find $path -iname *$filename*`
do
newpath=`echo $i | sed "s/$filename/$newfilename/g"`
sudo mv "$i" "$newpath"
echo "${count}: Renaming $i to $newpath"
let count++
done
但脚本将停止:
Liang/gideon_lisha/Gideon_samuel/Gideon_nathan.xml
因为它更改了文件夹名称,因此无法找到下一个路径。我不知道如何让脚本从内部运行到外部而不是从外部运行到内部。
答案 0 :(得分:2)
最后,我发现了anwser:
#!/bin/bash
path=$1
filename=$2
newfilename=$3
echo "We are finding '$filename' under the folder '$path'"
count=1
for i in `find $path -iname "*$filename*" | tac`
do
newpath=`echo $i | sed "s@\(.*\)$filename@\1$newfilename@i"`
sudo mv "$i" "$newpath"
echo "${count}: Renaming $i to $newpath"
let count++
done
非常感谢@susbarbatus!