2013-12-13 · in Tech Notes · 208 words

I find filter programs that have an "in place" option very useful — for example, sed -i or perl -i. But wouldn't it be nice if you could do that with any filter?

Meet inplace. Invoke this script as:

inplace COMMAND ... -- FILE ...

and it'll filter each FILE through COMMAND, using the usual trick of writing the output to a temporary file in the same directory and then renaming it over the original (unless the command failed).

Some examples:

inplace sort -u -- channels.conf
inplace jpegtran -rotate 90 -- *.jpg

This program is an implementation of the xargs Unix design pattern: it takes a command and a separate list of arguments to apply it to. However, there isn't a standard way to separate the command arguments from the remaining arguments, so the choice of -- is rather arbitrary. Some options I've seen:

I quite like the '' solution; having the option to read lines from stdin is also occasionally useful.