4

My computer at work has a tendency to generate an excessive amount of core files, which can be useful for debugging, but slowly take up all of the available space on my computer. I made this command to delete all the core files, given that they all start with "core."

locate /core. | grep -v core.h | grep -v core.c | grep -v core.py \
  | grep -v core.xml | grep -v core.txt | grep -v .gz \
  | grep -v core.so | grep -v chrome |sudo xargs rm

It works, but it's unwieldy and would delete say core.sh if that file existed on my computer. I think a better way would be:

  1. Use locate to find all the files starting with "core."
  2. Feed that list into file
  3. Make a list out of everything that file says is a core file. Assuming that a file is a core file if and only if the output of file file_name contains the phrase "ELF 64-bit LSB core file x86-64".
  4. Feed that to sudo xargs rm

But I don't know how to step three in that list.

3 Answers3

1

I have been using Linux every since Hardy Heron and I stumbled upon this one line script which will remove core dump files cleanly and safely. I don't remember where I originally found it, but it works great. Type the following line as super user of course:

find / -type f -name core -atime +1 -exec rm {} \;

That's it. Very simple and with proper substitution can be used to remove /tmp and /var/tmp files. The -atime attribute is variable so you can decide how many days of files you want to keep or not. Always try the simple solutions first.

andrew.46
  • 39,359
0

Combining the two answers:

find / -type f -name core -atime  -exec file {} \; | grep "ELF 64-bit LSB core file x86-64".
Flow
  • 2,476
0

I ended up doing it myself in python.

#!/usr/bin/env python2

from subprocess import PIPE, Popen

def is_core_file(filepath):
  ''' Use file command to determine if something is an actual core file.'''
  prog = Popen(["file", filepath], stdin=PIPE, stdout=PIPE)
  out = prog.communicate()[0]
  return "core file" in out

def main():
  prog = Popen(["sudo", "updatedb"], stdin=PIPE, stdout=PIPE)
  prog.communicate()
  prog = Popen(["locate", "/core."], stdin=PIPE, stdout=PIPE)
  cores = prog.communicate()[0].split('\n')
  for core in cores:
    if is_core_file(core):
      print("deleting " + core)
      prog = Popen(["sudo", "rm", "-f", core])

if __name__ == '__main__':
  main()

Edited to use main.