Coding⏱️ 3 min read📅 2026-06-15

How to Fix: How to solve "Broken Pipe" error when using awk with head

Broken pipe error when using awk with head to list oldest files in a directory.

Quick Answer: Use the `--ignore-zero-divisor` option with `head` to prevent it from closing stdin after reading 50 lines, or use `awk`'s output redirection (`>`) instead of piping to `head`.

The 'Broken Pipe' error occurs when the output of one command is piped into another, but the second command reads from stdin before the first command has finished writing to it. This can cause the first command to exit prematurely, resulting in an error.

This issue affects users who are using awk with head to process large amounts of data. The error can be frustrating because it may appear that the output is correct, but the actual problem lies in the timing of the commands.

⚠️ Common Causes

  • The primary reason for this error is that head reads from stdin until a certain number of lines have been read or a signal is received. If awk has not finished writing its output to stdout before head starts reading, it will close stdin prematurely, causing an error.
  • Another possible cause is that the input pipe is not properly buffered. This can lead to issues with data being lost or corrupted during transmission.

🔧 Proven Troubleshooting Steps

Using Process Substitution

  1. Step 1: Replace the command `ls -tr1 /a/path | awk -F ' ' -vpath=/prepend/path/ '{print path$1}'` with `ls -tr1 /a/path | awk -F ' ' -vpath=/prepend/path/ '{print path$1}' | xargs -I {} echo '{}'
  2. Step 2: The `xargs` command will read the output of awk and print it to stdout, rather than trying to process it further. This ensures that the data is fully processed before being written to stdout.
  3. Step 3: Alternatively, you can use `xargs -I {} echo '{}' | awk '{print $0}'` if you want to keep the original output format.

Using a Temporary File

  1. Step 1: Redirect the output of `ls -tr1 /a/path` to a temporary file, for example using `ls -tr1 /a/path > temp.txt`.
  2. Step 2: Modify the original command to pipe the output to the temporary file instead of stdout: `ls -tr1 /a/path | awk -F ' ' -vpath=/prepend/path/ '{print path$1}' | cat temp.txt`.
  3. Step 3: After processing the data, remove the temporary file using `rm temp.txt`. This ensures that no unnecessary files are left on disk.

🎯 Final Words

To resolve the 'Broken Pipe' error when using awk with head, you can use process substitution or redirect the output to a temporary file. Both methods ensure that the data is fully processed before being written to stdout, preventing the premature closure of stdin and resulting in an error.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions