0

I have two IP cameras which are sending image snapshots to an FTP server every 10 seconds, from 08:00 to 20:59. The directory structure looks like this:

`-- public_html
    `-- securitycams
        |-- cam01
        |   |-- 20190601
        |   |   |-- 08
        |   |   |   |-- 20190601_080000.jpg
        |   |   |   |-- 20190601_080010.jpg
        |   |   |   |-- 20190601_080020.jpg
        |   |   |   `-- ...
        |   |   |-- 09
        |   |   |   |-- 20190601_090000.jpg
        |   |   |   |-- 20190601_090010.jpg
        |   |   |   |-- 20190601_090020.jpg
        |   |   |   `-- ...
        |   |   |-- 10
        |   |   |-- 11
        |   |   |-- 12
        |   |   |-- 13
        |   |   |-- 14
        |   |   |-- 15
        |   |   |-- 16
        |   |   |-- 17
        |   |   |-- 18
        |   |   |-- 19
        |   |   `-- 20
        |   `-- 20190602
        |       |-- 08
        |       |   |-- 20190602_080000.jpg
        |       |   |-- 20190602_080010.jpg
        |       |   |-- 20190602_080020.jpg
        |       |   `-- ...
        |       |-- 09
        |       |   |-- 20190602_090000.jpg
        |       |   |-- 20190602_090010.jpg
        |       |   |-- 20190602_090020.jpg
        |       |   `-- ...
        |       |-- 10
        |       |-- 11
        |       |-- 12
        |       |-- 13
        |       |-- 14
        |       |-- 15
        |       |-- 16
        |       |-- 17
        |       |-- 18
        |       |-- 19
        |       `-- 20
        `-- cam02
        |   |-- 20190601
        |   |   |-- 08
        |   |   |   |-- 20190601_080000.jpg
        |   |   |   |-- 20190601_080010.jpg
        |   |   |   |-- 20190601_080020.jpg
        |   |   |   `-- ...
        |   |   |-- 09
        |   |   |   |-- 20190601_090000.jpg
        |   |   |   |-- 20190601_090010.jpg
        |   |   |   |-- 20190601_090020.jpg
        |   |   |   `-- ...
        |   |   |-- 10
        |   |   |-- 11
        |   |   |-- 12
        |   |   |-- 13
        |   |   |-- 14
        |   |   |-- 15
        |   |   |-- 16
        |   |   |-- 17
        |   |   |-- 18
        |   |   |-- 19
        |   |   `-- 20
        |   `-- 20190602
        |       |-- 08
        |       |   |-- 20190602_080000.jpg
        |       |   |-- 20190602_080010.jpg
        |       |   |-- 20190602_080020.jpg
        |       |   `-- ...
        |       |-- 09
        |       |   |-- 20190602_090000.jpg
        |       |   |-- 20190602_090010.jpg
        |       |   |-- 20190602_090020.jpg
        |       |   `-- ...
        |       |-- 10
        |       |-- 11
        |       |-- 12
        |       |-- 13
        |       |-- 14
        |       |-- 15
        |       |-- 16
        |       |-- 17
        |       |-- 18
        |       |-- 19
        |       `-- 20

I'm only interested in keeping a copy of the snapshots taken between:

08:00 and 09:59
13:00 and 14:59
19:00 and 20:59.

Therefore I would like to create a CronJob that will periodically delete all directories named "10", "11", "12", "15", "16", "17" and "18".

I'm a newbie in UNIX commands and searched a lot. I managed to create this command:

find /home/******/public_html/securitycams/* -depth \( -name "10" -or -name "11" -or -name "12" -or -name "15" -or -name "16" -or -name "17" -or -name "18" \) -type d -exec rm -r "{}" \;

It seems to work. But is that the correct way to go?

0

You command looks good to me. But you could shorten it a little bit:

  • -depth can be removed
  • \( -name ... -or -name ... \) can be shortened
  • instead of {} \; you could use {} + resulting in only one invocation of rm

find /home/******/public_html/securitycams -type d -name "1[0-25-8]" -exec rm -r {} +

If your cameras can be configured to store images between 08:00 and 20:59, maybe they can also be configured to only store images between 08:00 and 09:59, 13:00 and 14:59 and 19:00 and 20:59?

I think there is no "correct way" to do this. If a cron job works for you, why not?

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.