Delete Millions Of Files At Once In Linux

Posted Under: Linux, Shell on Nov 12, 2016
Usually if you want to remove files from Linux /bin/rm *.ext will do the trick. But there is a limit to what rm can do. If you tried to do this where number of files are in millions this command simple do not work and give an error Argument list too long. This happens when rm is called to remove a list of files/directories it literally concatenate the command in this manner before removing them

rm -i files file2 file3... fileN

so naturally there is limit to the argument list which resulted in Argument list too long. error.

To overcome this weakness and achieve speed while deleting such mammoth of files, I use the following command on my CentOS VPS

ls -1 | wc -l && time find . -type f -delete
this will result in
[tutorial@jinni ~]# ls -1 | wc -l && time find . -type f -delete
1517375 <---> total number of files
real    2m23.749s
user    0m1.097s
sys     0m35.723s

Beside deleting the files swiftly this command also show the time taken and the number of file there are.
© Tutorial Jinni