UNIX Command Spotlight

Uncommon tools worth knowing

$ sponge

Soak up all of stdin, then write it to a file — solving the classic "read and write to the same file" problem.

The Problem

In shell scripting, you've almost certainly hit this wall: you want to filter a file and write the result back to the same file. The naive approach silently destroys your data:

grep -v "debug" config.log > config.log

The shell truncates config.log before grep reads it. Result: an empty file. The usual workaround is a temporary file:

grep -v "debug" config.log > config.log.tmp && mv config.log.tmp config.log

It works, but it's verbose and error-prone (what if mv fails?).

The Solution: sponge

sponge — part of the moreutils package by Joey Hess — soaks up all of its standard input into memory, then writes it to the specified file. This means the entire pipeline reads from the original file before it gets overwritten.

grep -v "debug" config.log | sponge config.log

That's it. Clean, one-liner, no temp files.

Real-World Use Cases

Installation

# Debian / Ubuntu / Linux Mint
sudo apt install moreutils

# Alpine
sudo apk add moreutils

# macOS (Homebrew)
brew install moreutils

# FreeBSD
pkg install moreutils

# NetBSD
pkgin install moreutils

How It Works

sponge is conceptually simple: it reads all of stdin into a memory buffer, then opens the target file for writing and flushes the buffer. The key insight is the separation of read and write phases — the file is never opened until all upstream pipeline commands have finished reading.

Think of it like a kitchen sponge: it absorbs everything first, then you squeeze it out where you want.

Other Gems in moreutils

While you're at it, these siblings are worth knowing:

Reference

UNIX Command Spotlight — 2026-05-08 11:46 CEST