3

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.

2
  • This looks appropriate for a Systemd path job.
    – user535733
    Oct 16 at 2:03
  • New files come in to Folder A at random from outside the server. They need to sit in Folder A for 72 hours, then move to Folder B where they live forever.
    – Impulse0
    Oct 16 at 6:22
6

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 👍🏻

2
  • 1
    Nice and simple! I learned here that the quotes around the placeholder actually are never needed - names with spaces are also correctly handled without them.
    – vanadium
    Oct 16 at 9:17
  • Wow!! Asked and answered. Thank you so much!
    – Impulse0
    Oct 16 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.