Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

bash: Argument list too long: why rm, cp or mv fails on a large folder, and how to run the job in batches

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

When a command such as rm *.log stops with Argument list too long, the shell has expanded *.log into a list of file names that is larger than the kernel allows a new program to receive. The program (rm, cp, mv, ls, chown) was never started, so no file was touched. The usual remedy is to stop handing the whole list to one command: find can build the batches itself, or names can be fed to xargs.

What the error means

The text Argument list too long is the message the C library attaches to the error code E2BIG. The errno(3) manual page lists it as "E2BIG Argument list too long (POSIX.1-2001)". The error comes from the execve() system call, which starts a new program. The execve(2) manual page says E2BIG is returned when "The total number of bytes in the environment (envp) and argument list (argv) is too large, an argument or environment string is too long, or the full path of the executable is too long. The terminating null byte is counted as part of the string length."

So the limit covers the file names and the environment variables of the shell together, not only the file names.

The size of the limit depends on the kernel:

  • Before Linux 2.6.23 it was fixed at 32 pages, which is 128 kB on architectures with 4-kB pages.
  • On Linux 2.6.23 and later, for most architectures, the manual page says the total size is "limited to 1/4 of the allowed stack size", derived from the soft RLIMIT_STACK limit in force when execve() is called. It adds a floor of 32 pages, a limit of 32 pages for any single string, and a cap at 3/4 of 8 MiB.
  • The getrlimit(2) page confirms that the stack limit "also determines the amount of space used for the process's command-line arguments and environment variables".

The shell is where the list is built. The bash manual says that after word splitting, a word containing *, ? or [ "is regarded as a pattern, and replaced with an alphabetically sorted list of filenames matching the pattern". A folder with a few hundred thousand matching files produces a list far beyond the limit.

The sysconf(3) manual page describes ARG_MAX (queried at run time as _SC_ARG_MAX) as "The maximum length of the arguments to the exec(3) family of functions". The execve(2) page adds that since Linux 2.6.23, if the RLIMIT_STACK limit changes, the value reported by _SC_ARG_MAX changes with it. The getconf command takes "A system configuration variable, as defined by sysconf(3) or confstr(3)", so getconf ARG_MAX shows the current figure.

Likely causes

Cause What you see How to confirm
A glob matches too many files The error appears with *, *.log or similar, and the same command works in a smaller folder The number of matches times (average name length + 1) is larger than getconf ARG_MAX (the arithmetic is in the next section)
A very large environment adds to the total The error appears with a list that worked earlier, or only in one session env output is large (the next section measures it)
A lowered stack limit in the failing context The command works at your prompt but fails in a service, a container or a job ulimit -s in the failing context is smaller than at your prompt
One single argument is longer than 32 pages The error appears with few arguments, one being a very long string One value passed in quotes or from command substitution is very long
The path of the program is too long The error appears with no pattern at all The full path to the executable is unusually long

Prove the cause before changing anything

All of these only read, except that step 4 runs a two-line test script that you create. Run them in the folder that gives the error, in the same kind of session (shell, cron job, service) as the failure.

1. What is the limit here?

getconf ARG_MAX
ulimit -s

getconf ARG_MAX prints the limit in bytes. The bash manual describes ulimit -s as "The maximum stack size", and says values are "in 1024-byte increments" except for a few options that do not include -s. On a typical 8 MiB stack, ulimit -s prints 8192 and ARG_MAX is on the order of a quarter of 8,388,608 bytes. A small ulimit -s goes with a small ARG_MAX, as the execve(2) page describes.

2. How many files does the pattern match?

find . -maxdepth 1 -type f -name '*.log' -print | wc -l

find prints one name per line and wc -l counts lines ("print the newline counts" in the coreutils manual). File names that contain a newline would be counted twice, a limit the xargs manual raises when it says "Unix filenames can contain blanks and newlines". The find manual says global options such as -maxdepth should come "just before the first test", otherwise find issues a warning, so -maxdepth 1 is written first.

3. How large is the environment?

env | wc -c

wc -c prints the byte count. The environment counts towards the same limit, so a count in the hundreds of kilobytes, not a few kilobytes, is a cause by itself.

4. Is the failing context different from your prompt?

Read ulimit -s from inside the failing kind of session. Do this with a new, separate test script, not with a copy of the job: a copy of the job's script would run the whole job again, and the job may delete, move or send things. The test script holds only these three lines, so none of the job's own commands can run:

ulimit -s
getconf ARG_MAX
exit

Run the test script in the same kind of session as the failing job (for example from the same cron entry, or the same service setup) and read its output. The exit line ends the script before anything else can run. The execve(2) page says the limit is the soft stack limit "in force at the time of the execve() call", so a job started with a lower stack limit also gets a lower argument limit.

The arithmetic for a good result is: bytes needed = number of names x (average name length + 1), because each name carries a terminating null byte. If that figure is larger than ARG_MAX, the glob is the cause.

Fix

The aim is to run the command many times with part of the list each time. find does this with -exec ... {} +. The find manual says this variant runs the command on the selected files "but the command line is built by appending each selected file name at the end; the total number of invocations of the command will be much less than the number of matched files", built "in much the same way that xargs builds its command lines".

Do not start with deletion. The find manual documents a -delete action and advises testing a find command line "with -print prior to adding -delete". Deletion cannot be undone, so the steps below move the matching files to a new folder first. Remove them later under your normal retention process.

First save the list of exactly which files will be moved. Use your own folder and pattern:

find /srv/app/spool -maxdepth 1 -type f -name '*.log' -print > /root/moved-list.2026-12-01.txt

Read the start of that file and count its lines before going on:

head /root/moved-list.2026-12-01.txt
wc -l /root/moved-list.2026-12-01.txt

find -name '*.log' also matches names that start with a dot, which the shell pattern *.log skips, so the saved list shows exactly what will move. Continue only if it lists the files you intend to move.

Disruptive step: the next commands create a folder and move every matching file out of /srv/app/spool, so any program that expects those files in that folder will no longer find them until you move them back.

Create the destination, outside the folder being searched, and move the files in batches:

mkdir /srv/app/spool-old.2026-12-01 && find /srv/app/spool -maxdepth 1 -type f -name '*.log' -exec mv -t /srv/app/spool-old.2026-12-01 {} +

The && makes the move run only if mkdir succeeded, so the command stops if the folder already exists and the moved files are never mixed with others.

The -t option of mv is described in the coreutils manual as "move all SOURCE arguments into DIRECTORY", which is why the destination can come before the file names. The find manual says only one {} is allowed with the + form and it must stand last, directly before the +. Choose a destination name that does not exist yet.

To undo it: move the files back without overwriting any file of the same name, check the destination is empty, and only then remove the empty folder.

find /srv/app/spool-old.2026-12-01 -maxdepth 1 -type f -name '*.log' -exec mv -n -t /srv/app/spool {} +
find /srv/app/spool-old.2026-12-01 -maxdepth 1 -type f -print
rmdir /srv/app/spool-old.2026-12-01

The -n option is "do not overwrite an existing file". The rmdir manual says it removes directories "if they are empty", so it refuses a folder that still holds files. If the second command still lists names, do not run rmdir; find out why those files were not moved first.

The xargs manual describes the same idea for other commands: find ... -print0 | xargs -0 command, where -0 makes xargs read null-terminated names so spaces and newlines in file names are safe.

If the cause is a lowered stack limit in a service or job, raising the limit changes the whole process, and the manual pages for these commands do not describe the effect on memory. Prefer batching.

Example (illustrative): On web01, /srv/app/spool holds 100,000 files named like app-2026-11-30-000123.log, 25 characters each. rm *.log fails with Argument list too long. ulimit -s prints 8192 (KiB), so the stack is 8192 x 1024 = 8,388,608 bytes, and a quarter of that is 2,097,152 bytes, which is also what getconf ARG_MAX prints on this host. The names need 100,000 x (25 + 1) = 2,600,000 bytes, more than 2,097,152, before the environment is counted. After the list is saved and the move is run, /srv/app/spool-old.2026-12-01 holds the same 100,000 names that were in the saved list.

Confirm the fix

  1. The find command ran with no error and a zero exit status.
  2. A find count of *.log in the source folder prints 0, and the count in the destination equals the number of lines in the saved list file (wc -l of that file).
  3. The program that wrote the files can still write new ones where it expects them.

What to watch afterwards

Risks and limits

  • These commands have not been run or lab-tested.
  • The figures in the manual pages are for Linux. The limit on other systems differs, and the find and xargs described are the GNU versions.
  • The execve(2) manual page does not say whether the pointer array that execve() builds also counts towards the limit. The example therefore counts only name bytes and says the environment adds to them.
  • find -exec ... {} + returns a non-zero status if any invocation fails, and the find manual notes that some pending commands may not run if find itself meets an error. Check the destination count, not only the exit status.
  • The mv(1) page says nothing about moves between file systems. Choose a destination on the same file system as the source if you can, and check free space with df when you cannot.

Sources

  • execve(2) — Linux man-pages 6.19, 2026-02-08, accessed 1 December 2026
  • getrlimit(2) — Linux man-pages 6.19, 2026-02-08, accessed 1 December 2026
  • errno(3) — Linux man-pages 6.19, 2026-05-28, accessed 1 December 2026
  • sysconf(3) — Linux man-pages 6.19, 2026-02-08, accessed 1 December 2026
  • getconf(1) — Debian Manpages, bookworm (libc-bin 2.36), source updated 2025-03-06, accessed 1 December 2026
  • bash(1) — Debian Manpages, bookworm, source updated 2025-04-18, accessed 1 December 2026
  • find(1) — GNU findutils 2025-10-12, accessed 1 December 2026
  • xargs(1) — GNU findutils 2024-06-03, accessed 1 December 2026
  • mv(1) — GNU coreutils 9.11, April 2026, accessed 1 December 2026
  • wc(1) — GNU coreutils 9.11, April 2026, accessed 1 December 2026
  • env(1) — GNU coreutils 9.11, April 2026, accessed 1 December 2026
  • df(1) — GNU coreutils 9.11, April 2026, accessed 1 December 2026
  • mkdir(1) — GNU coreutils 9.11, April 2026, accessed 1 December 2026
  • rmdir(1) — GNU coreutils 9.11, April 2026, accessed 1 December 2026