0

I have a folder that contains so many files/folders inside it that even basic commands like du and find are crashing. I'd like to clean up some old files from it... but obviously I can't do that with the find command...


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

What command or trick can I use to delete files from that folder since find isn't working?

Ru Hasha
  • 876
  • 1
  • 7
  • 16
  • "Aborted" is not a segfault; SIGABRT != SIGSEGV. Still this sounds like a bug in `find`; it shouldn't be crashing even in exceptional situations like this. [This thread](https://stackoverflow.com/questions/3413166/when-does-a-process-get-sigabrt-signal-6) leads me to believe it might be running out of memory though, which makes a crash slightly more forgiveable. – Thomas Apr 27 '20 at 09:26
  • I also think it's running out of memory... Note that `du` suffers the same fate. – Ru Hasha Apr 27 '20 at 09:29
  • You could write your own C program using `opendir`, `readdir`, `stat` and `unlink`, which is easy to keep within constant memory usage (or at worst, linear in the depth of directory nesting). – Thomas Apr 27 '20 at 09:34
  • Or, if you don't want to write C, you can try some Perl: [Perl to the Rescue: Case Study of Deleting a Large Directory](http://blogs.perl.org/users/randal_l_schwartz/2011/03/perl-to-the-rescue-case-study-of-deleting-a-large-directory.html) – choroba Apr 27 '20 at 09:37
  • Or in python: https://stackoverflow.com/a/48393588/1930462 (search for "readdir" there). Might go this route if no-one finds a bash command that can do it. – Ru Hasha Apr 27 '20 at 09:42

2 Answers2

0

I believe the best way to go is using a simple for-loop: the problem is that find loads all found information in memory, and only once this is done, it starts deleting.
However, a loop can solve this:

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

Concerning the last modification date check, there are plenty of ways to do this, as explained here.

Dominique
  • 16,450
  • 15
  • 56
  • 112
0

For find command using the -exec option worked for me to delete the files.

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