SPONGE(1)

πŸ“¦
version 0.70 Β· 3 versions
  • 0.70 Β· debian bookworm, debian trixie, epel 10, epel 9, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu jammy, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking (latest)
  • 0.70 Β· alpine 3.24
  • 0.70 Β· arch rolling
, in debian bookworm, debian trixie, epel 10, epel 9, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu jammy, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– SPONGE(1) πŸ’–

okay bestie, gather round, because sponge is genuinely one of the most iconic little gremlins in the whole moreutils fam and i need you to get it 🧚✨


🌸 NAME 🌸

sponge β€” soak up standard input and write to a file 🫧


✨ SYNOPSIS ✨

sed '...' file | grep '...' | sponge [-a] file

πŸŽ€ DESCRIPTION πŸŽ€

so here's the vibe: sponge reads standard input and writes it out to the file you specified. that's it. that's the whole job. πŸ’…

but wait β€” the why is the juicy part. unlike a plain shell redirect, sponge soaks up all its input before writing the output file. it's literally a sponge babe, it absorbs everything first, then squeezes it out. 🧽

and why do we care?? because that means you can build pipelines that read from and write to the same file. the classic "wait, i can't redirect a file into itself" problem? yeah. solved. ate. no crumbs. πŸ‘‘

sponge also preserves the permissions of the output file if it already exists. your chmod work stays intact, we love to see it πŸ’–

when it can, sponge creates or updates the output file atomically by renaming a temp file into place. so clean, so safe, so main character energy ✨

⚠️ okay but heads up: that atomic glow-up cannot be done if TMPDIR is not in the same filesystem. the vibes are simply not transferable across filesystems, bestie 😭

and if the output file is a special file or symlink, the data will be written to it β€” but non-atomically. it's giving "we tried our best but the filesystem said no" πŸ’€

and if no file is specified, sponge just outputs to stdout. easy peasy πŸ“


πŸ’… OPTIONS πŸ’…

  • -a β€” Replace the file with a new file that contains the file's original content, with the standard input appended to it. and yes, this is done atomically when possible, because we stay classy 🌈

πŸ‘‘ AUTHOR πŸ‘‘

Colin Watson and Tollef Fog Heen β€” absolute legends, fr fr 🫢


and that's the whole page, babe! this one's from the moreutils collection, dated 2006-02-19 β€” a certified classic that's still slaying all these years later. go build a pipeline that reads and writes the same file, you deserve it πŸ’œπŸ¦‹β­

πŸ“¦ where this page comes from