【问题标题】:`find` command core dumps in directory with too many files`find` 命令核心转储到包含太多文件的目录中
【发布时间】:2020-04-27 09:24:36
【问题描述】:

我有一个文件夹,其中包含如此多的文件/文件夹,以至于即使像 dufind 这样的基本命令也会崩溃。我想从中清理一些旧文件...但显然我无法使用 find 命令来执行此操作...


# find /opt/graphite/storage/whisper -mtime +30 -type f -delete
Aborted (core dumped)

由于find 不起作用,我可以使用什么命令或技巧从该文件夹中删除文件?

【问题讨论】:

  • “中止”不是段错误; SIGABRT != SIGSEGV。这听起来还是find 中的一个错误;即使在这样的特殊情况下,它也不应该崩溃。 This thread 让我相信它可能内存不足,这使得崩溃稍微更容易原谅。
  • 我也认为它的内存不足...请注意 du 遭受同样的命运。
  • 您可以使用opendirreaddirstatunlink 编写自己的 C 程序,这很容易保持在恒定的内存使用量内(或者最坏的情况是深度线性目录嵌套)。
  • 或者,如果你不想写C,你可以尝试一些Perl:Perl to the Rescue: Case Study of Deleting a Large Directory
  • 或者在 python 中:stackoverflow.com/a/48393588/1930462(在那里搜索“readdir”)。如果没有人找到可以做到这一点的 bash 命令,可能会走这条路。

标签: linux find


【解决方案1】:

我认为最好的方法是使用一个简单的 for 循环:问题是 find 将所有找到的信息加载到内存中,并且只有在完成此操作后,它才会开始删除。
然而,一个循环可以解决这个问题:

for f in $(ls -a)
do 
  if <check_last_modification_date>($f)
  then rm -r $f
  fi
done

关于最后修改日期检查,有很多方法可以做到这一点,如 here 所述。

【讨论】:

    【解决方案2】:

    对于使用-exec 选项的find 命令,我可以删除文件。

    # find /opt/graphite/storage/whisper -mtime +30 -type f -exec rm -f {} \;
    

    【讨论】:

      猜你喜欢
      • 2011-01-05
      • 1970-01-01
      • 2017-11-17
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-04
      • 1970-01-01
      相关资源
      最近更新 更多