Linux 重命名嵌入日期的顺序命名文件 情况:
我有数百个zip文件,其名称中包含任意日期/时间(Linux 重命名嵌入日期的顺序命名文件 情况:,linux,command-line-interface,rename,archlinux,file-management,Linux,Command Line Interface,Rename,Archlinux,File Management,我有数百个zip文件,其名称中包含任意日期/时间(4-6-2021 12-34-09 AM.zip)。我需要按顺序获取所有这些文件,以便(0.zip、1.zip、2.zip等)在Linux cli系统中使用 我所尝试的: 我在阅读时尝试了ls-tr;donn=$((n+1));mv--“$i”“$(printf'%03d'$n”).zip”;完成,它几乎完成了我想要的,但似乎仍然不符合顺序(我认为它按照文件创建时的顺序,而不是文件名(这是我需要的)) 如果我能做到这一点,我的下一步将是将每个zi
4-6-2021 12-34-09 AM.zip
)。我需要按顺序获取所有这些文件,以便(0.zip、1.zip、2.zip等)在Linux cli系统中使用
我所尝试的:
我在阅读时尝试了ls-tr;donn=$((n+1));mv--“$i”“$(printf'%03d'$n”).zip”;完成
,它几乎完成了我想要的,但似乎仍然不符合顺序(我认为它按照文件创建时的顺序,而不是文件名(这是我需要的))
如果我能做到这一点,我的下一步将是将每个zip中的文件(是单个文件)重命名为zip文件的名称。我也不知道该怎么办
tl;博士
我把这些文件命名为一个奇怪的日期系统。我需要将日期按顺序并按顺序重命名,如0.zip 1.zip 2.zip等
。现在是凌晨3点,我不知道为什么我还在努力解决这个问题,我不知道如何将ZIP中的文件重命名为该序列号(请阅读上文了解更多详细信息)
提前谢谢 GNU awk是这里的一个选项,它将文件列表的结果重定向回awk:
awk '{
fil=$0; # Set a variable fil to the line
gsub("-"," ",$1); # Replace "-" for " " in the first space delimited field
split($1,map," "); # Split the first field into the array map, using " " as the delimiter
if (length(map[1])==1) {
map[1]="0"map[1] # If the length of the day is 1, pad out with "0"
};
if (length(map[2])==1) {
map[2]="0"map[2] # Do the same for month
}
$1=map[1]" "map[2]" "map[3]; # Rebuilt first field based on array values
gsub("-"," ",$2); # Change "-" for " " in time
map1[mktime($1" "$2)]=fil # Get epoch format of date/time using mktime function and use this as an index for array map1 with the original line (fil) as the value
}
END {
PROCINFO["sorted_in"]="@ind_num_asc"; # At the end of processing, set the array sorting to index number ascending
cnt=0; # Initialise a cnt variable
for (i in map1) {
print "mv \""map1[i]"\" \""cnt".zip\""; # Loop through map1 array printing values and using these values along with cnt to generate and print move command
cnt++
}
}' <(for fil in *AM.zip;do echo $fil;done)
awk'{
fil=$0;#将变量fil设置为行
gsub(“-”,“,$1);#在第一个空格分隔的字段中替换“-”for“”
split($1,map,“”;#将第一个字段拆分为数组映射,使用“”作为分隔符
如果(长度(映射[1])==1{
映射[1]=“0”映射[1]#如果一天的长度为1,则用“0”填充
};
如果(长度(映射[2])==1{
映射[2]=“0”映射[2]#对月份执行相同操作
}
$1=映射[1]“”映射[2]“”映射[3];#基于数组值重建第一个字段
gsub(“-”,“,”,$2);#及时将“-”改为“”
map1[mktime($1”“$2)]=fil#使用mktime函数获取日期/时间的历元格式,并将其用作数组map1的索引,原始行(fil)作为值
}
结束{
PROCINFO[“sorted_in”]=“ind_num_asc”;#在处理结束时,将数组排序设置为索引编号升序
cnt=0;#初始化cnt变量
对于(map1中的i){
打印“mv\”map1[i]“\”cnt.zip\”#循环通过map1数组打印值,并将这些值与cnt一起使用以生成和打印move命令
碳纳米管
}
}“这管用!非常感谢。您是否知道我可以将这些zip文件中的单个文件命名为zip文件名的方法(如在0.zip
中,zip中的jpg需要提取为name0.jpg
)?如果没有,那没关系,我就做一个新的问题线程;请解压缩-p'$f'>“tmp/${f%}.jpg”;完成
只是文件命名不正确。请尝试在*.zip中输入f;请解压缩-p'$f'>“tmp/${f%.zip}.jpg谢谢你!这非常有效!!!
awk '{ fil=$0;gsub("-"," ",$1);split($1,map," ");if (length(map[1])==1) { map[1]="0"map[1] };if (length(map[2])==1) { map[2]="0"map[2] }$1=map[1]" "map[2]" "map[3];gsub("-"," ",$2);map1[mktime($1" "$2)]=fil} END { PROCINFO["sorted_in"]="@ind_num_asc";cnt=0;for (i in map1) { print "mv \""map1[i]"\" \""cnt".zip\"";cnt++ } }' <(for fil in *AM.zip;do echo $fil;done) | bash