Use atomic_move to put the file, rather than shelling out

Fixes #4321