---
title: "Watch Out for Data Loss When Redirecting to a File in a Shell Script — Nick Janetakis"
url: https://daily.dev/posts/watch-out-for-data-loss-when-redirecting-to-a-file-in-a-shell-script-nick-janetakis-njtpgwsqc
source_url: https://nickjanetakis.com/blog/watch-out-for-data-loss-when-redirecting-to-a-file-in-a-shell-script
type: article
source: "Nick Janetakis"
published: 2026-07-07T11:36:16.126Z
updated: 2026-07-07T11:36:36.639Z
tags: ["shell", "bash"]
reading_time: 8
upvotes: 0
comments: 0
language: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

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

**[Nick Janetakis](https://daily.dev/sources/nickjanetakis)** · 8 min read · 0 upvotes · 0 comments

## Summary

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.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://nickjanetakis.com/blog/watch-out-for-data-loss-when-redirecting-to-a-file-in-a-shell-script>

## Similar posts on daily.dev

- [The one rsync mistake that could destroy your backups](https://daily.dev/posts/the-one-rsync-mistake-that-could-destroy-your-backups-s6biwkbt9) · All Things Open · 0 upvotes · 0 comments

---

Tags: [#shell](https://daily.dev/tags/shell), [#bash](https://daily.dev/tags/bash)

[View this post on daily.dev](https://daily.dev/posts/watch-out-for-data-loss-when-redirecting-to-a-file-in-a-shell-script-nick-janetakis-njtpgwsqc)
