How to Fix ‘Argument List Too Long’ (E2BIG) Error in Linux Terminal

Running standard command-line utilities such as rm *, cp *, mv *, or grep inside a Linux directory containing tens or hundreds of thousands of files only to be stopped by the shell error “/bin/rm: Argument list too long” (POSIX error E2BIG, errno 7) halts terminal operations immediately. Many users assume that this error originates from the command itself (e.g. rm). In reality, before an executable binary ever runs, the shell (Bash, Zsh, Dash) expands the wildcard asterisk (globbing) into a continuous space-separated string of file names. The Linux kernel’s execve() system call enforces a strict memory boundary—defined by the kernel parameter ARG_MAX—on the total memory allocated for command-line arguments and environment variables. When wildcard expansion exceeds this memory ceiling, the kernel refuses to execute the command. This technical guide explains kernel argument limits and provides step-by-step methods to manipulate massive file collections safely.

What Causes ‘Argument List Too Long’ in Linux?

The error occurs in early userspace before the target utility executes. Key triggers include:

  • Kernel ARG_MAX Memory Boundary: The Linux kernel restricts the maximum byte size of arguments and environment variables passed to a new process (traditionally 2MB to 6MB, defined in limits.h). When a directory contains 50,000 files whose combined pathname lengths exceed this limit, execve() returns E2BIG.
  • Unbounded Shell Wildcard Expansion (Globbing): Commands using bare asterisks (rm *, chmod 644 *) forcing the shell to construct an enormous single-line argument array in memory.
  • Swollen Environment Variables: Having massive PATH definitions, localized language strings, or embedded base64 keys consuming argument buffer headroom.
  • Deeply Nested Directory Traversal: Path expansion generating thousands of long absolute path strings that multiply argument byte totals.

Method 1: Inspect the System ARG_MAX Kernel Limit

Check the maximum argument capacity permitted by your current kernel configuration.

  1. Open your Terminal prompt.
  2. Inspect the ARG_MAX value using the getconf utility:
    getconf ARG_MAX
  3. The output will display the maximum byte limit (for example, 2097152 bytes, which equals 2 MB).
  4. To see how much memory your current environment variables already consume:
    env | wc -c
  5. The remaining difference represents the maximum allowable length for expanded filenames in a single command.

Method 2: Use find with -delete (Fastest for Deletions)

The find utility bypasses shell wildcard expansion entirely by evaluating files directly via filesystem system calls (readdir()).

  1. To safely delete hundreds of thousands of files in the active directory without hitting argument limits:
    find . -maxdepth 1 -type f -delete
    The -maxdepth 1 flag ensures the deletion applies only to the current folder and does not recurse into subdirectories.
  2. To delete specific file types (such as old log files or temporary sessions):
    find . -maxdepth 1 -type f -name "*.log" -delete
  3. Because find handles files individually without assembling a gigantic string in memory, it executes instantaneously without triggering E2BIG.

Method 3: Batch Execution Using find and xargs

When you need to perform operations other than deletion (such as moving, copying, or modifying permissions), piping into xargs divides the file list into safe chunks.

  1. To modify permissions across thousands of files safely:
    find . -maxdepth 1 -type f -print0 | xargs -0 chmod 644
    The -print0 and -0 flags handle file names containing spaces, quotes, or special characters safely using null-delimiter terminators.
  2. To move thousands of files to another directory:
    find . -maxdepth 1 -type f -name "*.txt" -print0 | xargs -0 -I {} mv {} /path/to/destination/
  3. xargs automatically measures your system’s ARG_MAX and feeds files to the command in optimal chunks below the kernel ceiling.

Method 4: Use Shell for / while Loops

Executing an iterative shell loop processes files one by one, completely avoiding argument buffer saturation.

  1. For deleting or moving files with progress monitoring:
    for file in *.log; do
        [ -e "$file" ] || continue
        rm "$file"
    done
  2. Alternatively, use a while loop reading from a pipeline:
    find . -maxdepth 1 -name "*.tmp" | while read -r line; do
        rm -f "$line"
    done
  3. While iterative loops take slightly longer than xargs due to spawning individual processes per file, they provide 100% reliability in constrained environments.

Method 5: Increase User Stack Size via ulimit

In modern Linux kernels (Linux 2.6.23 onwards), the ARG_MAX limit is dynamically tied to 25% of the user stack size limit (RLIMIT_STACK).

  1. Check your current user stack size:
    ulimit -s
  2. If the stack size is 8192 KB, you can temporarily increase it to 32768 KB:
    ulimit -s 32768
  3. Verify that ARG_MAX expanded proportionally:
    getconf ARG_MAX
  4. You can now run larger wildcard commands without hitting the E2BIG boundary.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.