我试图根据子目录的名称重命名子目录中的csv文件,同时还要添加_1.csv。例如:子目录中每个文件的filename_1.csv,filename_2.csv。
我写了一个bash脚本,但是没有给我正确的输出。
#!/bin/bash
num=1
parent=/whois/v10_cctlds/full/
newfolder=/whois/csv7/
mkdir "$newfolder"
for folder in "$parent"/*; do
if [[ -d "$folder" ]]; then
foldername="${folder##*/}"
for file in "$parent"/"$foldername"/*; do
filename="${file##*/}"
newfilename="$(foldername $num).csv"
cp "$file" "$newfolder"/"$newfilename"
done
fi
done
答案 0 :(得分:0)
尝试以下脚本:
#!/bin/bash
parent=/whois/v10_cctlds/full/
newfolder=/whois/csv7/
for i in `ls $parent`
do
let num=1
for a in `ls $parent/$i`
do mv $parent/$i/$a $newfolder"$i"_$num.csv
num=`expr "$num" + 1`
done
done
答案 1 :(得分:0)
我怀疑您想要这个,其中包含了我的评论。
#!/bin/bash
parent=/whois/v10_cctlds/full/
newfolder=/whois/csv7/
mkdir -p "$newfolder"
for folder in "$parent"/*/; do
num=0
foldername=$(basename "$folder")
for file in "$folder"/*; do
if [[ -f "$file" ]]; then
cp -v "$file" "$newfolder/${foldername}_$((++num)).csv"
fi
done
done