【问题标题】:Delete the files in current folder in the Linux based on the timestamp根据时间戳删除Linux当前文件夹中的文件
【发布时间】:2020-08-31 01:30:08
【问题描述】:

我想根据时间戳删除 Linux 中当前文件夹中的文件。使用下面的命令我可以过滤我想要删除的文件,但是不能使用 rm/delete 命令删除文件,我尝试了几种方法,比如我使用 xargs rm 命令和删除命令,没有一个工作,请帮助我。

  1. find -maxdepth 1 -type f -exec ls -l {} + | grep '00:00'
  2. ls -l | grep '00:00'

【问题讨论】:

  • “我尝试了多种方式”。请显示尝试的命令。

标签: linux


【解决方案1】:

您可以尝试执行上述操作,但也可以提取文件的路径。下面将删除 find 命令通过提供的时间戳找到的所有文件

      rm -rf `find -maxdepth 1 -type f -exec ls -l {} + | grep '00:00' | awk '{ print $9 }'`

【讨论】:

    【解决方案2】:

    您可以使用多种方法解决此问题:-

    1. 最有效的方法使您不必通过 xargs 管道传输所有内容,也不必处理带有空格或其他破坏性字符的文件名。

    find /path -type f -not -newermt "YYYY-MM-DD HH:MI:SS" -delete

    2)既然你找到了文件,你可以把你的时间戳当成一个文件,并用它作为参考点,但是请记住,它会用临时时间戳文件污染文件系统。

    例如2021 年 1 月 1 日:

    touch -t 201401010000 /tmp/2021-Jan-01-0000
    
    find /path -type f ! -newer /tmp/2021-Jan-01-0000 | xargs rm -rf 
    

    3)这不会用临时时间戳污染文件系统 find /path ! -newermt "YYYY-MM-DD HH:MM:SS" | xargs rm -rf

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2013-06-28
      • 2015-01-14
      • 1970-01-01
      相关资源
      最近更新 更多