Argument list too long (rm)

I bet you saw this error at least once, while trying to remove files. This problem usually occurs when you try to operate with a huge amount of files (e.g. move, remove). Bash creates a long command line, by feeding the list of files to a single command.

Fortunately there are multiple ways for overcoming this issue. The simplest one is to remove the whole directory, but this is too easy, right? Let's see what else can we do.

for loop

This one is pretty straight forward.

user@host> cd path/to/folder/
user@host> for i in *; do echo "$i"; done

This will output the whole list of files from the directory. Now let's remove some files, which are matching a wildcard.Assume we want to remove all .json files. The following example will do that.

user@host> for i in *.json; do rm "$i"; done

printf + xargs

In this example we will use printf for outputting the list of arguments (files to delete), and xargs for invoking the rm command.

user@host> printf '%s\0' * | xargs -0 rm

In this example we first output all files (notice the asterisk wildcard) with printf, and then invoking rm command with xargs against this arguments list.

find

The last thing we are going to try is find command.

First approach will consist of find command and it's exec argument:

user@host> find * -type f -exec rm -fv {} \;

In this example we search for all files (-type f) and execute against them the rm command.We are also calling rm with 2 arguments: -fv, which stand for force and verbose. Also exec command will substitute {} with the filename(s) found.

Another option with find command is -delete argument. All you need is just to pass -delete as an argument.

user@host> find * -type f -delete

Pretty simple, huh? What about verbose output and a wildcard? Let's remove all .json files from the current dir and output each filename.

user@host> find *.json -type f -print -delete

Let's go deeper. Assume you want to delete only those files which were modified more than 30 days ago.

user@host> find *.json -type f -print -delete -mtime +30

As you can see, rm-ing is good enough for a short list of files.But if you want to delete wide ranges of files, filtering them by name and/or by the modified date - find is a great solution.

About

My name is Eugene. I am a passioned web developer, in love with Node.js, with over 6yrs of experience.

My main domain of expertise is full cycle application development. I love building robust scalable Node.js applications.

I create all kind of applications - websites, API servers, realtime applications.

Skills / Technologies
Node.js, MongoDB, Mongoose, Socket.io, Mocha, Grunt, Git, LAMP, C/C++, Nginx
Contact