Monday, June 20, 2011

How to Delete a Big Number of Files in a Folder

If you’re using temporary files you should notice that after some time it is hard to delete them as their number is growing. If you have many files and try to delete them with a usual command rm -f you should receive something like this:
[root@server html]# rm -f *
-bash: /bin/rm: Argument list too long
In our case (Number of files in the folder is around 1 million ) if we run ls or other command, linux shell prompt hung and we need to start new terminal.
What to do when this occurs? You have to combine several Linux commands in order to empty this folder. Here is the command you should use; I will explain what does it do below.
[root@server html]# ls | grep .| xargs rm
This will do the following: the first command ls outputs the list of files. The output is being sent to grep command so you won’t see it in your console. Grep searches for files with “.” symbol (in the example given I think that all your files have so named extension – filenames contain a dot. So grep will extract all the file names that contain a dot and will throw them to the next function – xargs. xargs accepts these filenames and deletes them one by one. This allows to reduce server load and gives us the possibility to delete multiple files in one folder that are no longer necessary.