使用嵌入的日期重命名按顺序命名的文件

Renaming Sequentially Named Files with Date Embedded

情况:

我有数百个 zip 文件,其名称中混合了任意 date/time (4-6-2021 12-34-09 AM.zip)。我需要按顺序获取所有这些文件,以便在 Linux cli 系统中使用(0.zip、1.zip 2.zip 等)。

我尝试过的:

我试过 ls -tr | while read i; do n=$((n+1)); mv -- "$i" "$(printf '%03d' "$n").zip"; done 这几乎可以满足我的要求,但似乎仍然不正常(我认为它采用文件创建时间的顺序而不是文件名(这是我需要)).

如果我能完成这项工作,我的下一步将是将每个 zip 中的文件(是的单个文件)重命名为 zip 文件的名称。我也不确定该怎么做。

tl;博士

我用奇怪的日期系统命名了这些文件。我需要按顺序排列日期并按顺序重命名,如 0.zip 1.zip 2.zip etc。现在是 3:00 上午,我不知道为什么我还在尝试解决这个问题,而且我不知道如何将 zips 中的文件重命名为该序列号(阅读上面的更多详细信息).

提前致谢!

GNU awk 在这里是一个选项,将文件列表的结果重定向回 awk:

awk '{ 
        fil=[=10=];                                          # Set a variable fil to the line
        gsub("-"," ",);                                # Replace "-" for " " in the first space delimited field
        split(,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
        }
        =map[1]" "map[2]" "map[3];                     # Rebuilt first field based on array values
        gsub("-"," ",);                                # Change "-" for " " in time
        map1[mktime(" ")]=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=[=11=];gsub("-"," ",);split(,map," ");if (length(map[1])==1) { map[1]="0"map[1] };if (length(map[2])==1) { map[2]="0"map[2] }=map[1]" "map[2]" "map[3];gsub("-"," ",);map1[mktime(" ")]=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