Automate move new files after a set period of time

I receive files though a service at odd intervals and need to have them available in one folder for 72 hours before being archived in a different folder. I would like to automate this process. Ideally this would be a service that watches the "in-use" folder for new files, notes the time of their arrival, then moves them to the archive 3 days later.

I am currently running a crontab entry that runs every 72 hours to move the entire contents of the in-use folder to the archive folder. This causes a sync issue between the availability window for a given file and it moving when it is no longer needed.


Solution 1:

Something like this will work:

find /source/location -maxdepth 1 -mtime +3 -type f -exec mv "{}" /destination/location/ \;

How this works:

  • find will look for items in /source/location
  • -maxdepth 1 will limit the search to just the directory specified, ignoring subdirectories
  • -mtime +3 will limit results to 3 days or older
  • -type f will limit results to files only
  • -exec will run a command on those results, in this case it’s mv
  • "{}" is where the result of find will go
  • \; tells find that the command passed for exec is complete

Throw this in your cron job and have it run hourly if you wish. Only files that are 72 hours or older will be moved 👍🏻