set the tmp file for sed??

Cameron Simpson cs at zip.com.au
Sat Sep 26 21:27:56 UTC 2015


On 26Sep2015 16:39, bruce <badouglas at gmail.com> wrote:
>I know. This is probably really basic/subtle, but 'net searches are
>coming up empty.
>
>Doing testing of 'sed' it's generating tmp files in the base
>directory. Is there a way that the 'sed' tmp dir can be set to '/tmp'.
>
>A sample sed cmd would be
>
>sed -i '/foo/cat/g' abc.dat
>sed -i '/foo/d' abc.dat

I'm presuming you're using GNU sed (from the -i); if not, please say so.

IIRC, sed -i makes a temp file with the new content, then renames it to the 
original. This allows for an atomic in place edit, as opposed to a rewrite 
which will leave the file incomplete for a period (or might fail half way 
through, etc).

In order to ensure that a rename can be done the temp fie has to be on the same 
filesystem as the original, and the simple way of doing that reliably is to 
make the temp file in the same directory as the original.

If you need the temp file elsewhere (eg for size) you could use a wrapper. For 
example, bsed (disclaimer: yes, this is mine):

  https://bitbucket.org/cameron_simpson/css/src/tip/bin/bsed

which does a rewrite. It honours the $TMPDIR environment variable for the temp 
directory.

Cheers,
Cameron Simpson <cs at zip.com.au>


More information about the users mailing list