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 fromARG_MAX
inlimits.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))}'