LinuxCommandLibrary

sponge

Soak standard input before writing output

TLDR

Append file content to the source file

$ cat [path/to/file] | sponge -a [path/to/file]
copy

Remove all lines starting with # in a file
$ grep [[-v|--invert-match]] '^[#]' [path/to/file] | sponge [path/to/file]
copy

SYNOPSIS

sponge [options]

PARAMETERS

-a
    Append to instead of overwriting it. Creates the file if it does not already exist.

-o
    Overwrite to instead of appending to the file. Creates the file if it does not already exist.

-p
    Display the current version and exit.

DESCRIPTION

The sponge command reads all standard input before writing it to the specified file. This is useful when you want to modify a file in-place, and the command producing the output also reads from that file. Without sponge, the output may be truncated or corrupted, as the input file is being read and written to simultaneously.
sponge effectively acts as a buffer, consuming all input first, and only then writing the complete result to the target file. This prevents the common issue of a command reading from a file that it's actively overwriting. The command offers a safe alternative when redirecting the output of commands that operate on the same files they read from.

CAVEATS

sponge requires enough memory to store the entire input before writing it. Extremely large inputs could potentially lead to memory exhaustion. Also, it works with files, and not standard output. Therefore, you must redirect the output to a file for sponge to operate.

EXAMPLE

Replace all instances of 'foo' with 'bar' in file 'myfile.txt':
sed 's/foo/bar/g' myfile.txt | sponge myfile.txt
Appending the file contents with `sed`:
sed 's/foo/bar/g' myfile.txt | sponge -a myfile.txt

HISTORY

sponge was written by Robert Kaye and is a part of the moreutils package. Its primary use is to resolve the common problem of commands truncating or corrupting files when performing in-place modifications. It's designed to provide a simple and reliable way to buffer input before outputting it to a file.

SEE ALSO

tee(1), cat(1), sed(1), awk(1)

Copied to clipboard