Filtering Files by Time and Run a Command on Each File

Gavin Wiener
1 min readJul 28, 2019

TLDR:

find . -type f -newermt ‘5/1/2019 11:55:00’ -not -newermt ‘5/1/2019 12:55:00’ | sudo xargs -I ‘{}' mv ‘{}' 12

mv '{}' 12 is the command, “Move each file to a folder named 12”. This command was executed in the target directory and the destination folder was on the same level. By default, the argument is passed to the end of the command, therefore, a placeholder, '{}', was required.

The following example will find files created between a date-time range (greater than, less than), the date format is MM/DD/YYYY HH:mm:ss, and each of those files will be processed by xargs. The destination folder must exist already.

xargs processes each file one-by-one

Context

We developed a project which saved frames from multiple IP cameras’ via RTSP (“Real Time Streaming Protocol”), and executed motion and object detection. The object detection obviously had to be trained; we found better results when we trained the models will subsets of frames from different periods of the day e.g. sunrise, early morning, afternoon, the late afternoon, etc.

Sign-up here to receive automation tips, case studies, and guides on the mistakes you maybe making on your automation journey.

And follow me on Twitter where I post about my journey, automation, and business growth.

--

--

Gavin Wiener

I'm a coder and freelancer from South Africa with 5+ years experience in automations, integrations and API's using Python and Django.