3

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.

  • 1
    I would advise you to read through the manpage of find and use that instead of locate for several reasons. With find you can probably do everything at once. And please if you want to use file to check the type, provide us an example output of file that shows one of those core files you want to delete. – Byte Commander Sep 10 '15 at 19:30
  • Assume that file_name is a core file if and only if the output of file file_name contains the words "ELF 64-bit LSB core file x86-64". – Joshua Snider Sep 10 '15 at 19:41
  • You should edit your question to include this information. – Byte Commander Sep 10 '15 at 19:44
  • @ByteCommander: It is done. – Joshua Snider Sep 10 '15 at 19:46
  • Sooo . . . .from reading your line you seem to avoid deleting any *.c, *.py, *.xml files . . .The -v flag means reverse matching, i.e., ignore those extensions. Perhaps it would be more efficient to define which exact files you do want to keep – Sergiy Kolodyazhnyy Sep 10 '15 at 20:41
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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.