Operations 4 min read

Fastest Ways to Delete Hundreds of Thousands of Files on Linux

This article compares several Linux techniques—including rm, find with -exec or -delete, rsync, Python, and Perl—for removing 500,000 files, showing execution times and highlighting the most efficient method for massive file deletion.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Fastest Ways to Delete Hundreds of Thousands of Files on Linux

First, create 500,000 test files:

for i in $(seq 1 500000); do echo text >> $i.txt; done

1. rm time rm -f * The shell aborts with "argument list too long" and the command fails.

2. find with -exec time find ./ -type f -exec rm {} \; Execution takes about 43 minutes (49.86 s user, 1032.13 s system, total 43:19).

3. find with -delete time find ./ -type f -delete Completes in roughly 9 minutes (0.43 s user, 11.21 s system, total 9:13).

4. rsync

Create an empty directory blanktest and run: time rsync -a --delete blanktest/ test/ Finishes in about 0.59 s user, 7.86 s system, total 16.4 s.

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))

Runs in about 529 seconds (≈9 minutes).

6. Perl one‑liner

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

Finishes in roughly 1.28 s user, 7.23 s system, total 16.8 s.

Summary of results

rm – not usable for massive file counts.

find with -exec – ~43 minutes.

find with -delete – ~9 minutes.

rsync with --delete – ~16 seconds (fastest).

Perl one‑liner – ~16 seconds.

Python script – ~9 minutes.

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.

performancecommand-lineScriptingfile deletion
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.