1、想要重命名文件夹下的所有子文件夹的名称,所有的子文件夹的名称都加上文件夹的名称前缀
代码如下:
#!/bin/bash
mkdir ../newtest
ls -1 > ../newtest/dirName.txt
bName=$(basename `pwd`)
for name in $(cat ../newtest/dirName.txt)
do
#echo $bName
#echo $name
newName=$bName$name
#echo $newName
mv $name $newName
done
$(cp -rf ./$name ../newtest)
代码解释如下:
#!/bin/bash
mkdir ../newtest //创建一个新的测试目录
ls -1 > ../newtest/dirName.txt //将当前目录下的所有文件夹名列出,并保存在dirName.txt文件内
bName=$(basename `pwd`) //获取当前目录的名称
for name in $(cat ../newtest/dirName.txt) //遍历所有的子文件名
do
#echo $bName
#echo $name
newName=$bName$name //将子文件夹名加上“当前目录名称”的前缀
#echo $newName
mv $name $newName //将子文件夹名进行更改
done
$(cp -rf ./$name ../newtest) //将改名后的子文件夹拷贝到newtest目录下
2、想要重命名子文件夹下的所有子文件夹名称,所有的子文件夹的子文件夹名称都加上子文件夹的名称前缀
代码如下:
#!/bin/bash
mkdir ../newtest
ls -1 > ../newtest/dirName.txt
for name in $(cat ../newtest/dirName.txt)
do
cd $name
ls -1 > ../../newtest/subdirName.txt
subName=$(basename `pwd`)
echo $subName
for subname in $(cat ../../newtest/subdirName.txt)
do
echo $subname
newsubName=$subName$subname
echo $newsubName
mv -f $subname $newsubName
cp -rf ./$newsubName ../../newtest
done
cd ../
done
代码解释如下:
#!/bin/bash标签:重命名,name,..,所有,echo,文件夹,txt,newtest From: https://blog.51cto.com/u_15903730/5915590
mkdir ../newtest //创建一个新的测试目录
ls -1 > ../newtest/dirName.txt //将当前目录下的所有文件夹名列出,并保存在dirName.txt文件内
for name in $(cat ../newtest/dirName.txt) //遍历所有的子文件名
do
cd $name //进入子文件
ls -1 > ../../newtest/subdirName.txt //将当前目录下的所有文件夹名列出,并保存在subdirName.txt文件内
subName=$(basename `pwd`) //获取子文件夹的名称
echo $subName
for subname in $(cat ../../newtest/subdirName.txt) //遍历当前子文件夹的所有的子文件名
do
echo $subname
newsubName=$subName$subname //将子文件夹名加上“当前目录名称”的前缀
echo $newsubName
mv -f $subname $newsubName //将子文件夹名进行更改
cp -rf ./$newsubName ../../newtest //将改名后的子文件夹拷贝到newtest目录下
done
cd ../
done