Symptoms

This error message appears when you try to remove, move or copy a long list of files. When using your shell a command can only accept a limited number of arguments. When the number of arguments supplied to the command exceeds the permitted number of arguments an error message will appear:
-bash: /bin/rm: Argument list too long
linux command to find your limit for maximum arguments:
# getconf ARG_MAX
2097152
Example:
# rm *
-bash: /bin/rm: Argument list too long

Solution

There are number of solutions for this problem. Firstly, if there are no files within a directory we would like to keep, the best and fastest solution is to simply remove entire directory and recreate it again. Before you proceed make sure that the directory does not contain files you would like to keep and that you take a note regarding a permissions and ownership of this directory.
$ cd ../
$ ls -d mydirectory
$ rm -fr mydirectory
$ mkdir mydirectory
The other solution is to engage a for loop and remove all files one be one:
$ for i in *; do rm "$i"; done
or much faster by use of printf and xargs:
$ printf '%s__g5_token5da8037d2a67e' * | xargs -0 rm
or
$ time echo -n * | tr ' ' '__g5_token5da8037d2a6bb' | xargs -0 rm
However, the above solution has its own limitation as it also may remove files we would like to keep if no proper regular expression in in place. As for an example here we will remove only *.txt files:
$ for i in *.txt; do rm "$i"; done
The last and proposed solution is to use regex to split all file into smaller batches. Find a similar pattern in all files and split them into a smaller groups. For example first remove,copy or move all files which start with a and have extension txt, after that all files which start with b and so on.:
$ rm a*.txt
$ rm b*.txt
You can also do the above for all letters using a for loop:
$ for i in $( echo {a..z} ); do rm $i*.txt ; done
ARE YOU LOOKING FOR A LINUX JOB?
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed on LinuxCareers.com.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in: