<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/watch-out-for-data-loss-when-redirecting-to-a-file-in-a-shell-script-nick-janetakis-njtpgwsqc" -->

---
title: Watch Out for Data Loss When Redirecting to a File in a...
description: 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...
canonical: https://daily.dev/posts/watch-out-for-data-loss-when-redirecting-to-a-file-in-a-shell-script-nick-janetakis-njtpgwsqc
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Watch Out for Data Loss When Redirecting to a File in a Shell Script — Nick Janetakis | daily.dev
og:description: 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...
og:url: https://daily.dev/posts/watch-out-for-data-loss-when-redirecting-to-a-file-in-a-shell-script-nick-janetakis-njtpgwsqc
og:image: https://api.daily.dev/og/posts/NjtpgwsQc.png
og:image:alt: Watch Out for Data Loss When Redirecting to a File in a Shell Script — Nick Janetakis
og:image:width: 1200
og:image:height: 630
og:locale: 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
- [Redirect Multiple Commands or an Entire Script to a File — Nick Janetakis](https://daily.dev/posts/redirect-multiple-commands-or-an-entire-script-to-a-file-nick-janetakis-wjo6swtdp) · Nick Janetakis · 1 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)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"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","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/watch-out-for-data-loss-when-redirecting-to-a-file-in-a-shell-script-nick-janetakis-njtpgwsqc"},"datePublished":"2026-07-07T11:36:16.126Z","dateModified":"2026-07-07T11:36:36.639Z","description":"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...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/3737be36d5fb9f3ae8dfdfd1b405dc0e?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/3737be36d5fb9f3ae8dfdfd1b405dc0e?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Nick Janetakis","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Nick Janetakis","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/05b22e1c43b541059edfaba1ccc944e6","url":"https://daily.dev/sources/nickjanetakis"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/watch-out-for-data-loss-when-redirecting-to-a-file-in-a-shell-script-nick-janetakis-njtpgwsqc","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"shell,bash","timeRequired":"PT8M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Nick Janetakis","item":"https://daily.dev/sources/nickjanetakis"},{"@type":"ListItem","position":3,"name":"Watch Out for Data Loss When Redirecting to a File in a Shell Script — Nick Janetakis"}]}
```

