Sunday, August 24, 2008

Linux: "Argument list too long" error when trying to use '/bin/rm' command

This error is not very common but this is something that you will see for sure in your daily routine if you are a Linux user. Lets say you are working with Apache webserver and there comes a situation where you have to delete log files for Apache.

[root @ guest logs]# pwd
/u01/app/oracle/product/j2ee/Apache/Apache/logs
[root @ guest logs]# ls -l | grep log | wc -l
5195
Try issuing the "rm" command to delete those long list of files
[root @ guest logs] # rm *log*
/bin/rm: Argument list too long.
If that returns an error message regarding "Argument list too long" then there are
alternatives to remove the files. It seems that the rm command can't deal with such
number of arguments. Fortunately there are some workarounds for this problem.

You can combine rm with find:

find . | xargs rm -f

Or if you want to delete all the files in the directory with a single stroke issue the following code:

ls | xargs rm -f

No comments: