Search code examples
linuxfilecronmove

A crontab to move completed uploads from one dir to another?


I'm using the following crontab, once an hour, to move any files with the .mp3 extension from the dir "webupload" to the dir "complete" :

60 * * * * find usr/webupload -type f -maxdepth 1 -name "*.mp3" -exec mv {} usr/webupload/complete \;

The problem is that "webupload" contains lots of partial files being transferred.

I've read about a lot of different ways to achieve this but I think i'm more confused now than I was when I started!

What is the best practice or easiest way to only move the completed uploads?

Many thanks :)


Solution

  • It's going to be hard to tell when a file is completely written unless it is renamed when the download is completed, but you could change your find command and add -mmin +1 so that it only looks for files which have been modified more than 1 minutes ago (meaning that download is likely completed). Also, you should use / at the beginning of your paths rather than the relative paths your using:

    60 * * * * find /usr/webupload -type f -mmin +1 -maxdepth 1 -name "*.mp3" -exec mv {} /usr/webupload/complete \;
    

    You could obviously make the modification time longer (eg. 10 minutes -mmin +10) if you want to be more certain that the file has been downloaded.