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.
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 -deleteCompleted 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 secondsConclusion: Using rsync with the --delete option is the quickest and most convenient way to delete a massive number of small files on Linux.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
