Shell file redirection truncates the destination file immediately when a pipeline starts, before any data is written. This creates a risk of data loss if the pipeline fails midway — for example, overwriting a valid database backup with an empty or partial file. The safe pattern is to redirect output to a temporary file first, then use `mv` to atomically rename it to the final destination (ensuring both files are on the same filesystem). The post uses `strace` to verify the truncation behavior and confirm atomic renames via `renameat2` syscalls.
Table of contents
# Showing the Problem# Using strace to Pry Deeper# Writing Files Safely# Demo Video381 Impressions