我需要对测试目录中的文件进行排序和重命名,以便根据粗体数字,文件将被重命名为 1-5,每组 5 个。我能够对它们进行排序,但现在需要将它们重命名为 1,2,3,4 ,或 5。这是我到目前为止所拥有的:
find . -maxdepth 1 -type f -name '*.txt' | sort -nt_ -k2,2 -k7,7
./FoilHole_6862563_Data_6834945_6834947_20190608_255634_Image.txt
./FoilHole_6862563_Data_6834952_6834954_20190608_255710_Image.txt
./FoilHole_6862563_Data_6834959_6834961_20190608_255748_Image.txt
./FoilHole_6862563_Data_6834935_6834937_20190608_255827_Image.txt
./FoilHole_6862563_Data_6834967_6834969_20190608_255906_Image.txt
./FoilHole_6862568_Data_6834945_6834947_20190608_060123_Image.txt
./FoilHole_6862568_Data_6834952_6834954_20190608_060159_Image.txt
./FoilHole_6862568_Data_6834959_6834961_20190608_360237_Image.txt
./FoilHole_6862568_Data_6834935_6834937_20190608_460316_Image.txt
./FoilHole_6862568_Data_6834967_6834969_20190608_560354_Image.txt
我现在需要做的是将它们重命名为:
./FoilHole_6862563_Data_6834945_6834947_20190608_1_Image.txt
./FoilHole_6862563_Data_6834952_6834954_20190608_2_Image.txt
./FoilHole_6862563_Data_6834959_6834961_20190608_3_Image.txt
./FoilHole_6862563_Data_6834935_6834937_20190608_4_Image.txt
./FoilHole_6862563_Data_6834967_6834969_20190608_5_Image.txt
./FoilHole_6862568_Data_6834945_6834947_20190608_1_Image.txt
./FoilHole_6862568_Data_6834952_6834954_20190608_2_Image.txt
./FoilHole_6862568_Data_6834959_6834961_20190608_3_Image.txt
./FoilHole_6862568_Data_6834935_6834937_20190608_4_Image.txt
./FoilHole_6862568_Data_6834967_6834969_20190608_5_Image.txt
答案1
作为黑客bash
脚本:
#!/bin/bash
while IFS= read -r file; do
newprefix=${file%%_Data*}
if [ "$prefix" != "$newprefix" ]; then
cnt=1
prefix=$newprefix
fi
newfile=$(echo "$file" | sed "s/[0-9]*\(_Image.txt\)/$cnt\1/")
echo mv "$file" "$newfile"
((cnt++))
done < <(find . -maxdepth 1 -type f -name '*.txt' | sort -nt_ -k2,2 -k7,7)
输出:
$ ./script.sh
mv ./FoilHole_6862563_Data_6834945_6834947_20190608_255634_Image.txt ./FoilHole_6862563_Data_6834945_6834947_20190608_1_Image.txt
mv ./FoilHole_6862563_Data_6834952_6834954_20190608_255710_Image.txt ./FoilHole_6862563_Data_6834952_6834954_20190608_2_Image.txt
mv ./FoilHole_6862563_Data_6834959_6834961_20190608_255748_Image.txt ./FoilHole_6862563_Data_6834959_6834961_20190608_3_Image.txt
mv ./FoilHole_6862563_Data_6834935_6834937_20190608_255827_Image.txt ./FoilHole_6862563_Data_6834935_6834937_20190608_4_Image.txt
mv ./FoilHole_6862563_Data_6834967_6834969_20190608_255906_Image.txt ./FoilHole_6862563_Data_6834967_6834969_20190608_5_Image.txt
mv ./FoilHole_6862568_Data_6834945_6834947_20190608_060123_Image.txt ./FoilHole_6862568_Data_6834945_6834947_20190608_1_Image.txt
mv ./FoilHole_6862568_Data_6834952_6834954_20190608_060159_Image.txt ./FoilHole_6862568_Data_6834952_6834954_20190608_2_Image.txt
mv ./FoilHole_6862568_Data_6834959_6834961_20190608_360237_Image.txt ./FoilHole_6862568_Data_6834959_6834961_20190608_3_Image.txt
mv ./FoilHole_6862568_Data_6834935_6834937_20190608_460316_Image.txt ./FoilHole_6862568_Data_6834935_6834937_20190608_4_Image.txt
mv ./FoilHole_6862568_Data_6834967_6834969_20190608_560354_Image.txt ./FoilHole_6862568_Data_6834967_6834969_20190608_5_Image.txt
每当文件名前缀发生变化时,计数器变量cnt
就会重置为,例如当更改为时。要真正重命名文件,您必须从脚本中删除 。1
./FoilHole_6862563
./FoilHole_6862568
echo
答案2
您可以将bash
算术与rename
命令结合起来:
i=0
find . -maxdepth 1 -type f -name '*.txt' |
sort -nt_ -k2,2 -k7,7 |
while read filename ; do
rename -v "s/[0-9]+_Image/$((i%5+1))_I/" ${filename}
i=$((i+1))
done
-n
使用命令选项进行尝试rename
,它只显示正在执行的操作。
警告:
文件名来自管道输入,变量filename
是使用read
此处的内置函数设置的。这将要如果您的文件名与您在问题中指定的格式不匹配,例如带有空格、换行符等的文件名,则会中断。因此请注意,您需要使用不同的方法,例如 的find
选项-print0
与xargs -0
.
在这种情况下,使用计数器的帮助程序文件并将?
其用作变量替换:
echo -1 > file_counter
find -name '*txt' -print0 |
sort -znt_ -k2,2 -k7,7 |
xargs -I ? -0 bash -c '
i=$(($(cat file_counter)+1))
rename -n -v "s/[0-9]+_Image/$(($i%5+1))/" ?
echo $i > file_counter
'