r/CLI • • 2d ago

What should a batch command do when 99 files succeed and one fails?

For a command that processes 100 files and fails on one, what output makes the next step obvious? Do you prefer a non-zero exit code with a separate list of failures, a summary on stderr, or something else? I'm especially interested in retrying just the failed items without parsing a wall of logs.

1 Upvotes

2 comments sorted by

2

u/illegal_appendix 2d ago

Non-zero exit for sure. Then a short line at the end like 99 ok, 1 failed, with the failed file names on their own lines so I can feed them back in and rerun just those.

1

u/ysnows123 1d ago

There's good prior art to copy here, and people already know it:

  • Give partial failure its own exit code, separate from "nothing worked". rsync exits 23 for a partial transfer, GNU xargs returns 123 when any one run failed, and GNU parallel returns the number of failed jobs (up to 100). A script can then tell "retry a few" apart from "it's broken".
  • For independent files, keep going by default and offer fail-fast as a flag. make does the reverse (it stops at the first error, and -k/--keep-going continues), which suits builds where later steps depend on earlier ones.
  • The retry path is where most tools fall short, and GNU parallel gets it right: --joblog jobs.log writes one line per item with its exit status, and running again with --resume-failed (or --retry-failed) redoes only the failures. A lighter version of the same idea is a flag like --failed-to failed.txt that writes just the failed paths, one per line, plus a -0 option for NUL-separated output since filenames can contain newlines. Retrying is then yourtool --from failed.txt.
  • Keep stdout for results and send the summary ("99 ok, 1 failed" plus each failed path with its reason) to stderr, so piping the output doesn't mix the report in. If people will script against it, add --json.