When “rm *” doesn’t work

We’ve all deleted files in a folder using rm *. But have you ever had it not work you might have come across an error like this:

sudo rm *
-bash: /usr/bin/sudo: Argument list too long

What’s happening here? Well, this is a consequence of the Linux kernel’s limitation on the number of arguments that can be passed to a command via the exec system call. In my particular environment this limit was 2097152 (2MB) which is shown by this command:

getconf ARG_MAX

The Solution

Thankfully a handy little tool called xargs can help us here. This tool reads items from the standard input, delimited by blanks (which can be
protected with double or single quotes or a backslash) or
newlines, and executes the command in the argument.

ls | xargs sudo rm

In this way rm is executed multiple times avoiding the system exec call limit. You might assume that the command will be run once per file but xargs is smarter than that. Running once per file is pretty poor for performance in most cases. Instead, the command line for command is built up until it reaches a system-defined limit (unless the -n and -L options are used). The specified command will be invoked as many times as necessary to use up the list of input items. You can see how these limits are built using the --show-limits option.

xargs --show-limits
Your environment variables take up 2190 bytes
POSIX upper limit on argument length (this system): 2092914
POSIX smallest allowable upper limit on argument length (all systems): 4096
Maximum length of command we could actually use: 2090724
Size of command buffer we are actually using: 131072
Maximum parallelism (--max-procs must be no greater): 2147483647

Leave a Reply

Your email address will not be published. Required fields are marked *