Nick Janetakis
Read post

Watch Out for Data Loss When Redirecting to a File in a Shell Script — Nick Janetakis

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.

    #shell#bash
Jul 07•8m read time•From nickjanetakis.com
Post cover image
Table of contents
# Showing the Problem# Using strace to Pry Deeper# Writing Files Safely# Demo Video
381 Impressions
Nick Janetakis's image
Nick Janetakis

Nick Janetakis's blog provides insights into web development, Docker, and technology tutorials. Read...

77 Followers

•

1.3K Upvotes

Would you recommend this post?

Copy link
WhatsApp
Facebook
X
New Squad
  • © 2026 Daily Dev Ltd.
  • Guidelines
  • Explore
  • Tags
  • Sources
  • Squads
  • Leaderboard