Score:3

Automate move new files after a set period of time

br flag

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.

user535733 avatar
cn flag
This looks appropriate for a [Systemd path](https://www.freedesktop.org/software/systemd/man/systemd.path.html) job.
br flag
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.
Score:6
in flag

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

vanadium avatar
cn flag
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.
br flag
Wow!! Asked and answered. Thank you so much!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.