top of page
book of spells.webp

Start your journey towards mastering the CLI with our FREE Command Line Book of Spells.

By entering your email address you agree to receive emails from Command Line Wizardry. We'll respect your privacy and you can unsubscribe at any time.

Editing in Place - Redirecting Command Output to the Same Source File

  • Writer: Paul Troncone
    Paul Troncone
  • Jun 23, 2021
  • 2 min read

Updated: Jun 30, 2021


Sometimes you may want to use the command line to edit a file in place without creating a temporary file. Some commands, such as sed, natively support this functionality, but most do not.







For example, suppose you had the following file scores.txt:

Last      First   Score
Smith     Dave    95
Jones     Tom     74
Cooper    Ann     85

scores.txt


You can use the cut command to remove the middle column and output only the last name and score:

$ cut -f1,3 scores.txt

Last    Score
Smith   95
Jones   74
Cooper  85

But suppose you wanted to save the output back to scores.txt. You cannot simply use redirection to solve this problem:

$ cut -f1,3 scores.txt > scores.txt

Instead of outputting the result of the cut command to scores.txt, the redirection above will result in scores.txt being overwritten with an empty file.


One solution is to output the result of the cut command to a temporary file, delete the original scores.txt file, and then rename the temporary file. Here is a one-line example:

cut -f1,3 scores.txt > tmp.txt; rm scores.txt; mv tmp.txt scores.txt

While this works, it is quite lengthy and requires the generation of a temporary file.


To help simplify this and eliminate the need for a temporary file I created a bash script, ow, to output a stream to a specified file, including overwriting the original source file of a command pipeline.

#!/usr/bin/env bash
#Command Line Wizardry
#commandlinewizardry.com

#Overwrite
#Version 1.0

if [ $# == 0 ] 
then
    echo "$0: Output file name required"
    exit 1
fi

out=""

while IFS= read LineIn
do
    out="$out$LineIn\n"
done

echo -en $out>$1

To use it, just put it at the end of a command pipeline and specify a file to write the stream to. To overwrite the original source file specify the source filename as the argument for ow.


In the example below the output of the cut command is piped into ow and the result is written back to scores.txt.

 cut -f1,3 scores.txt | ow scores.txt

You can download the ow script from our GitHub repository. To make it even more useful be sure to add ow to your path and make it executable so the script can be accessed from anywhere on your command line.


Note that ow is written primary for text output. If your command pipeline outputs binary data ow may not process it correctly.

7 Comments


Sam Carter
Sam Carter
Jun 24

The idea of redirecting command output back into the same source file is pretty interesting, especially the caution around how easily things can go wrong if you’re not careful with overwriting content in place. I liked the way the post breaks down why this isn’t just a simple “one-liner trick,” but something that requires a clear understanding of how file streams and redirection actually behave under the hood. It made me think about how often small command-line conveniences can hide a lot of complexity when you look closer. I was also wondering how people typically test this safely before using it on important files, since one mistake could be hard to undo. It’s a bit like working through unfamiliar technical…

Like

funded firm
funded firm
Jun 16

Fundedfirm offers a practical Funding Pips Calculator designed to help traders estimate position size, risk exposure, profit targets, and account growth with ease. The tool simplifies important trading calculations, making daily planning faster and more accurate. By entering a few key values, users can quickly understand potential outcomes before placing trades. It supports better decision-making and helps maintain consistency in risk management. The calculator is suitable for traders working with funded accounts and evaluation programs. Its clean interface and easy-to-use features make it a valuable resource for anyone looking to improve trading efficiency and performance. 

Like

Janay j . Flora
Janay j . Flora
May 11

As a PhD student, I found this post on editing files in place surprisingly relatable. Research often feels like redirecting effort back into the same document until everything becomes clearer. During my college days, I struggled to balance studies and work, which is why I now help others through Write My Assignment while working part-time at Last Minute Assignments. I’m deeply conscious about academics and believe the right guidance can turn complicated tasks into something much more manageable and meaningful. Good Post

Edited
Like

Elliott Lawery
Elliott Lawery
Mar 31

editing in place and redirecting command output back to the same source file, and it’s a useful explanation of why you can’t simply redirect a command’s output to overwrite the file you’re reading from for example, trying something like cut -f1,3 scores.txt > scores.txt doesn’t work because the shell truncates the file before the command reads it, which ends up emptying the file instead of saving the modified content back into it, and that’s something that surprises a lot of people when they first run into it.  The article goes on to show how some tools like sed support in-place editing with options like -i, which handle this by creating a temporary file behind the scenes and then replacing the original, an…

Edited
Like

Raghav Roy
Raghav Roy
Aug 30, 2025

I’m thoroughly impressed by the Aerocity Call Girls Service attention to detail and the effort put forth to ensure my happiness and comfort. I will certainly seek out the same experience again.

Like
bottom of page