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.
You can use multiple methods to delete large number of files in a directory. The commonly used is the one you mentioned find comamnd with delete option.
You can even use perl, rsync, etc. The below post does a comparitive study of file deletion performance.