Blog

rm -f *: “Argument List too long”

Tags: Argument List too longfile deletionfindrm -f *

Published on: October 19, 2014 by Sreejith K R

rm -f *:  “Argument List too long”

Scenario:

Why?

 “Argument List too long” error occurs when there is a large number of arguments passed to a command.We have come across this issue on delete files using rm command. This is caused because the command can not accept the arguments passed to it in full. There is a limit on the size of arguments passed to a command. The environment variables “ARG_MAX” or “_POSIX_ARG_MAX” defines the argument list.

We can check the limit using “getconf ARG_MAX” or “getconf _POSIX_ARG_MAX”.

Below are the results obtained


$getconf ARG_MAX

2097152


$ getconf _POSIX_ARG_MAX
2097152

This is a kernel limit defined in /usr/include/linux/limits.h.

Please note that the result of getconf ARG_MAX  depends on the stack size and may be different from ARG_MAX in limits.h

Explanation for the issue

The rm -rf */Diorectory command will take all the files in the directory as its arguments. So the command becomes :- “rm -rf file1 file2 file3 … fileN”. The argument list size becomes more than the limit and the error occurs.

Solution:

There are multiple methods to overcome this situation :

1. Find and delete using exec

a. Removes all files in current(.) directory.

 find . -type f -exec rm -fv {} ; 

b. Remove all files and directories in current(.) directory.

Use the switch -rf at your own risk !!!

 find . -depth ! -path . -exec rm -rf {} \; 

Deleting files using embedded delete in find is faster

 find . -type f -delete 

To display the file name being deleted :

 find . -type f -print -delete 

Note: These commands takes too much disk I/O.

2. Using a shell loop(too slow)

 for i in *; do rm -f $i; done 

To display what is being deleted :

 for i in $(echo *); do echo "Deleting : $i"; rm -f $i; 

3. Using Perl to delete files

 perl -e 'for(<*>){((stat)[9]<(unlink))}' 

Get 24/7 expert server management

Category : General, Howtos, Linux, Troubleshooting

Sreejith K R

Sreejith K R

Sreejith is an early adopter of upcoming technologies and has got immense interest on exploring its features for improving productivity. He is quite proficient in various scripting languages and loves to enhance work flow through development of various automation scripts. He is video game junkie and is a perfect choice for the position of a game tester :-)

You may also read:

Comments

Add new commentSIGN IN

Let's Connect

Categories

Your Cart

Cart is empty.

Subtotal
₹0.00
APPLY
0
Send this to a friend