情况:
我有成百上千的压缩文件,它们的名称中混合了任意的日期/时间(4-6-2021 12-34-09 AM.zip)。我需要得到所有这些文件的顺序,以便(0.zip,1.zip,2.zip等)与Linux cli系统。
我尝试过的:
我尝试过ls -tr | while read i; do n=$((n+1)); mv -- "$i" "$(printf '%03d' "$n").zip"; done,它几乎可以做我想要的事情,但似乎仍然没有顺序(我认为它采用的是文件创建时间的顺序,而不是文件名(这就是我需要的))。
如果我能做到这一点,我的下一步将是将每个zip中的文件(是单个文件)重命名为zip文件的名称。我也不确定我该怎么做。
tl;dr
我用一个奇怪的日期系统给这些文件命名。我需要的日期是在顺序和重命名像0.zip 1.zip 2.zip etc的顺序。现在是凌晨3点,我不知道为什么我还在尝试解决这个问题,我也不知道如何将zip中的文件重命名为该序列号(请阅读上面的内容以了解更多详细信息)。
提前感谢!
发布于 2021-04-06 16:25:22
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)对print命令的打印方式感到满意后,将结果通过管道传输到bash中,然后:
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) | bashhttps://stackoverflow.com/questions/66963867
复制相似问题