LinuxCommandLibrary

sponge

Soak up stdin before writing to file

TLDR

Overwrite file with pipeline output

$ cat [file.txt] | sort | sponge [file.txt]
copy
Filter and save in place
$ grep "[pattern]" [file.txt] | sponge [file.txt]
copy
Transform and overwrite
$ sed 's/old/new/g' [file.txt] | sponge [file.txt]
copy
Append instead of overwrite
$ echo "new line" | sponge -a [file.txt]
copy

SYNOPSIS

sponge [options] file

DESCRIPTION

sponge reads standard input and writes to a file. Unlike shell redirection, it reads all input before opening the output file, allowing safe in-place modifications.
The tool is part of moreutils and solves the problem of using a file as both input and output in a pipeline.

PARAMETERS

-a

Append to file instead of overwriting.

EXAMPLES

Without sponge (fails):

$ sort file.txt > file.txt  # Results in empty file!
copy
With sponge (works):
$ sort file.txt | sponge file.txt
copy

CAVEATS

Loads entire input into memory. Very large files may exhaust memory. Part of moreutils package. Creates temp file internally.

HISTORY

sponge is part of moreutils, a collection of Unix tools created by Joey Hess. It addresses a common shell scripting pitfall where redirection to the input file truncates it before reading.

SEE ALSO

tee(1), cat(1), moreutils(7)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community