Operations 4 min read

Which Linux Deletion Method Is Fastest for Half a Million Files?

This article creates 500,000 small files on a Linux system and benchmarks various deletion techniques—including rm, find, rsync, Python, and Perl—showing their execution times and concluding which method offers the best performance and convenience.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Which Linux Deletion Method Is Fastest for Half a Million Files?

We created 500,000 tiny text files and measured how quickly each tool could delete them on a Linux machine.

1. rm

$ time rm -f *

The command fails with "argument list too long"; the reported timing is 3.63 s user, 0.29 s system, 98 % CPU, total 3.985 s, but it cannot handle the massive file count.

2. find with -exec rm

$ time find ./ -type f -exec rm {} \;

This method took roughly 43 minutes (49.86 s user, 1032.13 s system, 41 % CPU, total 43:19.17).

3. find with -delete

$ time find ./ -type f -delete

Completed in about 9 minutes (0.43 s user, 11.21 s system, 2 % CPU, total 9:13.38).

4. rsync --delete

# First create an empty directory blanktest
$ time rsync -a --delete blanktest/ test/

Finished in roughly 16 seconds (0.59 s user, 7.86 s system, 51 % CPU, total 16.418 s), making it both fast and convenient.

5. Python script

import os, timeit

def main():
    for pathname, dirnames, filenames in os.walk('/home/username/test'):
        for filename in filenames:
            file = os.path.join(pathname, filename)
            os.remove(file)

if __name__ == '__main__':
    t = timeit.Timer('main()', 'from __main__ import main')
    print(t.timeit(1))

The script ran for about 9 minutes (529.3 seconds).

6. Perl one‑liner

$ time perl -e 'for(<*>) { ((stat)[9] < (unlink)) }'

Completed in roughly 16 seconds (1.28 s user, 7.23 s system, 50 % CPU, total 16.784 s), the fastest among the scripting approaches.

Result Summary

rm: not usable for huge file count
find -exec: 43 minutes
find -delete: 9 minutes
Perl: 16 seconds
Python: 9 minutes
rsync --delete: 16 seconds

Conclusion: Using rsync with the --delete option is the quickest and most convenient way to delete a massive number of small files on Linux.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Performance TestingScriptingrsyncfindfile deletion
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.