Renaming a temporary file to the final destination does not always work. Specifically, if the source and destination paths are on different file systems, you will get the following error invalid cross-device link Instead, create temporary file in the destination directory.