I want to do a /home search and remove for files named error_log that are over 5mb

I use this now but it doesn't factor space:

find /home -type f -name error_log -exec rm -f {} \;

Any idea how to add the 5 mb limit here?

Thanks!

Dani AI

Generated

A concise, safe workflow to remove only the error_log files larger than 5 MB (and notes on pitfalls).

’s original find + rm is fine as a concept, and ’s Python wrapper is a good way to get exact-byte control if needed. For most cases the easiest, safest approach is: (1) list candidates first with exact sizes so you can review them; (2) when you’ve confirmed the list, delete. Use null-terminated output if filenames might contain spaces or newlines. The find builtin can print file sizes in bytes, which makes filtering unambiguous. (gnu.org)

Example listing step (review before deleting):

find /home -type f -name 'error_log' -printf '%s\t%p\n' | awk '$1 > 5242880 {print $2}'

This prints files whose st_size is greater than 5 1024 1024 bytes. Using find’s -printf avoids surprises from locale/ls formatting. (gnu.org)

When ready to remove, prefer find’s built-in delete action (it’s both faster and avoids a fork for each file). Running a print-first dry run then the delete is a common pattern. For large trees or odd filenames, prefer null-terminated flows (find -print0 → xargs -0) or find -delete. (gnu.org)

Important detail about -size: the M suffix means mebibytes (1,048,576 bytes) and find rounds sizes up to the next unit; that can affect borderline files. If you need exact-byte comparisons, use the c suffix (bytes), for example -size +5242880c. Always confirm with a listing before destructive actions. (gnu.org)

Security/performance notes: prefer -delete or -execdir over -exec … rm when running as root or on shared temp areas, and avoid piping un-reviewed lists into rm. If your system’s find lacks -printf or -delete (some BSD variants differ), the Python approach by or a small shell loop with stat will give the same control.

Recommended Answers

All 3 Replies

You could use a python script named lescript

#!/usr/bin/env python3
# -*-coding: utf8-*-
import os
import sys

if __name__ == '__main__':
    size = int(sys.argv[1])
    for filename in sys.argv[2:]:
        if os.stat(filename).st_size > size:
            try:
                os.unlink(filename)
            except:
                print('Ignoring', filename)

Then invoke

find /home -type f -name error_log -exec lescript 5242880 {} +

Awesome, I think I found a way like this:

find /home -size +5m -type f -name error_log -exec rm -f {} \; | sort -u

I'm going to try these too though, thanks again.

use Ccleaner disk analyze option, and when if finish analyzing scroll down until you find 5 mb files and start looking by file name, this is an alternatif way of doing it ..good luck mate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.