如何比 rm -rf 更快地删除文件?

如何比 rm -rf 更快地删除文件?

可能重复:
php tmp 错误中的数百万个文件-如何删除?

有没有比使用 rm -rf 命令更快地删除文件夹/文件的方法?

看来我的磁盘里有数十亿个文件(php5 的会话)没有在 cron 中删除,所以我需要手动删除它们,但这需要几个小时,而且仍然无法减少数量。谢谢。

我的命令:rm -rf /var/lib/php5/*

还尝试了这些命令:

find /var/lib/php5 -name "sess_*" -exec rm {} \;

perl -e 'chdir "/var/lib/php5/" or die; opendir D, "."; while ($n = readdir D) { unlink $n }'

答案1

php5 应该带有一个默认的 cron 作业来删除会话文件。

在 Debian/Ubuntu 中,它类似于以下内容(直接从 Ubuntu 12.04 LTS 复制)

/etc/cron.d/php5

# /etc/cron.d/php5: crontab fragment for php5
#  This purges session files older than X, where X is defined in seconds
#  as the largest value of session.gc_maxlifetime from all your php.ini
#  files, or 24 minutes if not defined.  See /usr/lib/php5/maxlifetime

# Look for and purge old sessions every 30 minutes
09,39 *     * * *     root   [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -depth -mindepth 1 -maxdepth 1 -type f -cmin +$(/usr/lib/php5/maxlifetime) ! -execdir fuser -s {} 2>/dev/null \; -delete

每半小时运行一次,并根据以下情况删除过期的会话:会话 gc_maxlifetimephp.ini

因此您应该执行以下操作:

  1. 检查您是否有上述 cron 作业文件。如果缺少,请添加。
  2. 检查值会话 gc_maxlifetime/etc/php5/apache2/php.ini

    默认值为会话 gc_maxlifetime在 Ubuntu 上是 1440 秒 = 24 分钟

    session.gc_maxlifetime = 1440
    
  3. 如果以上 2 项看起来正常,请尝试手动运行 cron 作业中的命令行。这将在屏幕上打印所有错误。

  4. 在 /var/log/syslog 中查找 cron 错误。查看它们是否与 php 有关。

关于已经存在的数十亿个会话文件,您现在必须手动删除它们。

控制当前局势

service apache2 stop
mv /var/lib/php5 /var/lib/php5.delete
mkdir /var/lib/php5
chmod 733 /var/lib/php5
chmod o+t /var/lib/php5
service apache2 start

然后删除/var/lib/php5.delete。这可能需要几个小时。同时,留意新目录中的文件数量/var/lib/php5目录。如果它以异常方式增加,那么除了删除文件之外,您还遇到了其他问题。

手动运行 cron 作业命令行

只需将部分放在在命令提示符中,如下所示

[ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -depth -mindepth 1 -maxdepth 1 -type f -cmin +$(/usr/lib/php5/maxlifetime) ! -execdir fuser -s {} 2>/dev/null \; -delete

答案2

最快的方法可能是:

cd /var/lib/php5
ls -f | xargs -d "\n" rm

还:

cd /var/lib/php5
for i in {1..999}
do
   find . -type f | head -1000 | xargs rm
done

如果你喜欢perl

perl -e 'chdir "/var/lib/php5" or die; opendir D, "."; while ($n = readdir D) { unlink $n }'

相关内容