Search code examples
bashhttp-redirecttruncate

run a bash command and redirect stdout/stderr to different files each limited to 50K


I can do it without the file size limit:

bash_script >${TMP}/stdout 2>${TMP}/stderr

I could just truncate them afterwards, but what I want is for them never to get bigger than the size limit in the first place...


Solution

  • Use head -c50kB to limit a pipe to its first 50'000 bytes.

    bash_script > >(head -c50kB > "$tmp/stdout") 2> >(head -c50kB > "$tmp/stderr")
    

    or

    bash_script 2> >(head -c50kB > "$tmp/stderr") | head -c50kB > "$tmp/stdout"