How calculate the number of files which can be passed as arguments to some command for batch processing?

Let xargs do the calculation for you.

printf '%s\0' files/* | xargs -0 mv -t new_files_dir

Your question seems to assume that there's an actual "limit of number of arguments", while in fact it's a combination of two limits:

  1. the sum of the string lengths of the command line arguments and environment variables including their terminating NUL bytes.

  2. the maximum string length of a single command-line argument.

For instance, you may be able to call a command with 200000 single-letter arguments, with 100000 two-letter arguments, but not with a single argument of more than 128k bytes.

Assuming the xargs from GNU coreutils, xargs --show-limits </dev/null will show what those limits are on your system.

On any system, xargs will not use the maximum limits of your system when constructing command lines, but will pick something reasonable (it would make no sense whatsoever to put strain on the system in that way).


If it really matters, you could write your very own batch-move program in C, which takes e.g. the file list as standard input and does the moving using the relevant Unix system calls.

If not, the "find out the limits and work up to that" is exactly what xargs(1) (GNU version on Linux, here) does. I doubt it very much that you'll get much faster.

Tags:

Linux

Bash

Ulimit