【问题标题】:How to list a file by filename as a date?如何按文件名列出文件作为日期?
【发布时间】:2017-05-06 09:34:21
【问题描述】:

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

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

我想在我的 RHEL 环境中从 sysdate 压缩这些文件 - 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 天 *)对不起我的英语

代码的想法是什么? 谢谢

【问题讨论】:

  • 切片、拆分、追加、转换、连接和字符串。我就是这么想的。
  • 是的,伙计。我只想知道像 ls -lh 这样的脚本,它可以像我上面提到的那样列出
  • 你试过什么?我们这里的大多数人都很乐意帮助你提高你的手艺,但作为短期无偿编程人员不太乐意。在MCVE 中向我们展示您迄今为止的工作、您期望的结果和您得到的结果,我们将帮助您解决问题。
  • 大家好,谢谢您的建议。我已经列出了这样的列表,用于压缩格式为年和月的文件,这是我的代码,当我为所有具有年月的 txt 文件压缩文件时 dir=date '+%Y%m%d'tanggal=date --date="$(date +%Y-%m-15) -1 month" '+%Y%m'base=/info_share/ProcessedSrc/CDC/ARCHIVE/ F_CC 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 天 *)对不起对于我的英语

标签: bash shell unix rhel


【解决方案1】:

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

【讨论】:

  • 嗨@sjsam 感谢您的回复。我将尝试实现您的代码并尽快返回结果!非常感谢
  • @BlackRabbit1703 :不客气,如果有帮助,请接受答案。
  • 嘿,上尉@sjsam!你救了我的一天,伙计!非常感谢你的队长!
  • @海军上将@BlackRabbit1703:你摇滚!提前问候四季。不要忘记在 SO winterbash 上收集一些帽子。
猜你喜欢
  • 2021-02-25
  • 1970-01-01
  • 2011-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-11
  • 2010-11-27
相关资源
最近更新 更多