<p>I have two machines, speed and mass. speed has a fast Internet connection and is running a crawler which downloads a lot of files to disk. mass has a lot of disk space. I want to move the files from speed to mass after they're done downloading. Ideally, I'd just run:</p>

<pre><code>$ rsync --remove-source-files speed:/var/crawldir .
</code></pre>

<p>but I worry that rsync will unlink a source file that hasn't finished downloading yet. (I looked at the source code and I didn't see anything protecting against this.) Any suggestions?</p>

[{'answer_id': 48500, 'author': 'Paul Tomblin', 'author_id': 3333, 'author_profile': 'https://Stackoverflow.com/users/3333', 'pm_score': 3, 'selected': False, 'text': "<p>How much control do you have over the download process?  If you roll your own, you can have the file being downloaded go to a temp directory or have a temporary name until it's finished downloading, and then mv it to the correct name when it's done.  If you're using third party software, then you don't have as much control, but you still might be able to do the temp directory thing.</p>\n"}, {'answer_id': 48517, 'author': 'Jason Cohen', 'author_id': 4926, 'author_profile': 'https://Stackoverflow.com/users/4926', 'pm_score': 3, 'selected': False, 'text': "<p>It seems to me the problem is <em>transferring</em> a file before it's complete, not that you're deleting it.</p>\n\n<p>If this is Linux, it's possible for a file to be open by process A and process B can unlink the file.  There's no error, but of course A is wasting its time.  Therefore, the fact that rsync deletes the source file is not a problem.</p>\n\n<p>The problem is rsync deletes the source file only after it's copied, and if it's still being written to disk you'll have a partial file.</p>\n\n<p>How about this: Mount <code>mass</code> as a remote file system (NFS would work) in <code>speed</code>.  Then just web-crawl the files directly.</p>\n"}, {'answer_id': 48628, 'author': 'Grey Panther', 'author_id': 1265, 'author_profile': 'https://Stackoverflow.com/users/1265', 'pm_score': 2, 'selected': False, 'text': "<p>Rsync can exclude files matching certain patters. Even if you can't modify it to make it download files to a temporary directory, maybe it has a convention of naming the files differently during download (for example: <code>foo.downloading</code> while downloading for a file named <code>foo</code>) and you can use this property to exclude files which are still being downloaded from being copied.</p>\n"}, {'answer_id': 93280, 'author': 'pjz', 'author_id': 8002, 'author_profile': 'https://Stackoverflow.com/users/8002', 'pm_score': 2, 'selected': False, 'text': "<p>If you have control over the crawling process, or it has predictable output, the above solutions (storing in a tempfile until finished, then mv'ing to the completed-downloads place, or ignoring files with a '.downloading' kind of name) might work.  If all of that is beyond your control, you can make sure that the file is not opened by any process by doing 'lsof $filename' and checking if there's a result. Clearly if no one has the file open, it's safe to move it over.  </p>\n"}]
['https://Stackoverflow.com/questions/48491', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4300/']