如何按文件名列出文件作为日期?

How to list a file by filename as a date?

我有格式如下的文件名列表_ddmmyyyy.txt 例如:

filea_01122016.txt
filea_02122016.txt
filea_03122016.txt
filea_04122016.txt
filea_05122016.txt

而且我想从 sysdate 压缩这些文件 - 在我的 RHEL 环境中需要 3 天。假设今天是 2016 年 12 月 5 日,我想压缩这些文件从 12 月 2 日开始,向后 12 月 1 日开始。因为我使用的日期来自文件名而不是(系统)创建的文件的时间戳。

我读了一些他们正在使用 find 实用程序的教程,但在我的例子中,我使用的是文件名中的日期。

我已经做了一个这样的机制来压缩整整一个月的文本

dir=`date '+%Y%m%d'`

tanggal=`date --date="$(date +%Y-%m-15) -1 month" '+%Y%m'`

base=/inf_shr/ProcessedSrc/CDC/ARCHIVE/F_ABC
cd $base

mkdir $dir
cp ../../F_CC/*${tanggal}* $dir

tar cvf - $dir | gzip -9 - > ${tanggal}_F_CC.tar.gz

rm -rf $dir
rm -rf ../../F_CC/*${tanggal}*

现在我想用 sysdate 压缩文件 - 3 天 *)对不起我的英语

关于代码的想法? 谢谢

TL;DR 但下面是我的方法

$ ls # listing the initial content
filea_17122016.txt  filea_19122016.txt  filea_21122016.txt
filea_18122016.txt  filea_20122016.txt  script.sh
$ cat script.sh  # Ok, here is the script I wrote
#!/bin/bash
now="$(date +%s)"
for file in filea_*
# Mind, if recursive globbing required put ** instead of *
# after you do set -s globstar
do
  date=${file#filea_}
  date=${date%.txt}
  y=$(cut -b 5- <<<"$date") # We use cut to trim year month and date separately
  m=$(cut -b 3-4 <<<"$date")
  d=$(cut -b 1-2 <<<"$date")
  date=$(date -d "$y-$m-$d" +%s) # Calulating the time in seconds from epoch, using the date string where we use the above calculated values
  dif=$(( (now-date)/(3600*24) )) #Calculating the difference in dates
  if [ "0$dif" -ge 3 ]
  then
    zip mybackup.zip "$file" #If the file is 3 or more days old, the add it to zip
    #Note if the zip file already exists, files will be appended
    rm "$file" # remove the file
  fi
done
$ ./script.sh # Script doing its job
  adding: filea_17122016.txt (stored 0%)
  adding: filea_18122016.txt (stored 0%)
$ unzip -l mybackup.zip  #listing the contents of the zip file
Archive:  mybackup.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2016-12-21 11:38   filea_17122016.txt
        0  2016-12-21 11:38   filea_18122016.txt
---------                     -------
        0                     2 files
$ ls # listing the left over content, which includes the newly created zip
filea_19122016.txt  filea_21122016.txt  script.sh
filea_20122016.txt  mybackup.zip
$ # Job done