Deleting large number of files in linux

Deleting large number of files in linux

You might have often encountered the

Argument list is too long

message when trying to rm -rf large amount of files. This happens often on large servers when trying to clean up the tmp folder. This is because the rm command has a pretty low count of supported parameters (filenames) while the tmp folder can become host to a terribly high number of files (millions in a period of years).

To bypass this issue, find can be used instead.

find . -type f -delete

To also list the filenames of the files as they are processed, add -print

find . -type f -print -delete

If one wishes to delete only a certain kind of files, find supports an extra parameter, -name ‘string’

find . -name 'a*' -type f -print -delete

will delete all files with filenames starting with “a”.

PS: Replace -type f with -type d if you wish to delete folders (directories) instead of files.

One comment

Leave a Reply