Retrying a flaky command

Back to list2025-08-27archived
You

How do I retry a network command a few times in bash?

ChatGPT

A small loop with backoff beats reaching for a tool:

for attempt in 1 2 3 4 5; do
  if curl -fsS "$url" -o "$out"; then break; fi
  sleep $(( attempt * 2 ))
done

The -f is essential: without it curl exits zero on a 500 and writes the error page into your output file.

You

What is the fastest way to check whether two directories match?

ChatGPT

Checksums of the file list, not a byte-by-byte diff:

cd a && find . -type f -exec sha256sum {} + | sort -k2 > /tmp/a.txt
cd ../b && find . -type f -exec sha256sum {} + | sort -k2 > /tmp/b.txt
diff /tmp/a.txt /tmp/b.txt

This also tells you which side differs, which diff -r on large trees takes far longer to reach.