How to Delete a Big Number of Files in a Folder – Linux Command

By | June 28, 2008

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

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.

2 thoughts on “How to Delete a Big Number of Files in a Folder – Linux Command

  1. Pingback: User links about "xargs" on iLinkShare

  2. Pingback: User links about "xargs" on iLinkShare

Comments are closed.