Symptom

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:

# rm *
-bash: /bin/rm: Argument list too long

Caused By:

getconf ARG_MAX 
2097152

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\0' * | xargs -0 rm

or

$ time echo -n * | tr ' ' '\0' | 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

This article was last modified: June 3, 2016, 10:29 a.m.

0 Comments

Please log in to leave a comment.

Add or change tags.

A comma-separated list of tags.

Share

Hacker News

Top