Delete files that have passed a certain period of time with Raspberry PI

Overview

This time, if you build a CCTV system with Raspberry PI, create a command and register it in Cron to automatically delete image files and video files that have passed a certain period of time.

Extract files after a certain period of time

The following command will display files that are older than one day. Current date: 2020/09/29 Get files older than one day: Specify the argument "+1". Get the file for 28 days: Specify the argument from "+1" to "0".

■ Command

find /var/lib/motion -name '*.*' -mtime +1

■ Execution result

./03-20200926145041-02.jpg
./03-20200926145042-00.jpg
./03-20200926145042-01.jpg
./03-20200926145042-02.jpg
./03-20200926145043-00.jpg
./03-20200926145043-01.jpg
./04-20200927183825.avi
./04-20200927183825-00.jpg
./04-20200927183825-01.jpg
./04-20200927183825-02.jpg
./04-20200927183826-00.jpg
./04-20200927183826-01.jpg
./04-20200927183826-02.jpg
./04-20200927183826-03.jpg

Delete files after a certain period of time

If you add the argument "-delete" to the above command, it will be deleted. Let's check with the following command.

■ Command

find /var/lib/motion -name '*.*' -mtime +1 -delete

■ Execution result The following error is displayed. The reason is that you do not have permission to delete the directory. In this case, use the "sudo" command with "root" privileges to delete the file.

 find: cannot delete � /var/lib/motion/17-20200927213443-01.jpg. ‥: Permission denied

■ Delete again using the sudo command Execute the following command.

pi@raspberrypi:/var/lib/motion $ sudo find /var/lib/motion -name '*.*' -mtime +1 -delete
pi@raspberrypi:/var/lib/motion $

■ Check if deleted Confirm that the file is deleted and not displayed in the command execution result below.

pi@raspberrypi:/var/lib/motion $ sudo find /var/lib/motion -name '*.*' -mtime +1
pi@raspberrypi:/var/lib/motion $

Settings for executing commands on a regular basis

On Linux, use cron to execute certain commands or programs on a regular basis. For Windows, it's the same as Task Scheduler.

■ Command It will be displayed the first time you execute it as shown in the screen below. I like the VI editor, so I choose 2.

pi@raspberrypi:/var/lib/motion $ crontab -e
no crontab for pi - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.tiny
  3. /bin/ed

Choose 1-3 [1]:2

■ cron settings Add the following line. Every day at 1:00, the command to delete files that are older than 31 days is executed.

Reference: cron configuration guide https://www.express.nec.co.jp/linux/distributions/knowledge/system/crond.html

 m h  dom mon dow   command
0 1 * * * sudo find /var/lib/motion -name '*.*' -mtime +31 -delete

At the end

If you apply this content this time, you can compress the file, attach it to an email, and automatically upload it to Slack chat. I'll write a program to attach to emails and Slack in the near future.

Recommended Posts

Delete files that have passed a certain period of time with Raspberry PI
How to use the Slack API using Python to delete messages that have passed a certain period of time for a specific user on a specific channel
Easily make a TweetBot that notifies you of temperature and humidity with Raspberry Pi + DHT11.
Using a webcam with Raspberry Pi
Randomly play the movie on ChromeCast for a certain period of time
Use shutil to delete all folders with a small number of files
Build a Tensorflow environment with Raspberry Pi [2020]
Make a wash-drying timer with a Raspberry Pi
Operate an oscilloscope with a Raspberry Pi
Create a car meter with raspberry pi
[Python] Create a list of date and time (datetime type) for a certain period
How to make a Raspberry Pi that speaks the tweets of the specified user
CSV output of pulse data with Raspberry Pi (CSV output)
Get CPU information of Raspberry Pi with Python
One-liner that outputs 10000 digits of pi with Python
Measure CPU temperature of Raspberry Pi with Python
Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
A memorandum when making a surveillance camera with Raspberry Pi
Let's operate GPIO of Raspberry Pi with Python CGI
Make a note of what you want to do in the future with Raspberry Pi
[Raspberry Pi] Scraping of web pages that cannot be obtained with python requests + Beautiful Soup
Give the history command a date and time and collect the history files of all users with a script
GPGPU with Raspberry Pi
DigitalSignage with Raspberry Pi
Create a web surveillance camera with Raspberry Pi and OpenCV
I tried running Movidius NCS with python of Raspberry Pi3
Production of temperature control system with Raspberry Pi and ESP32 (1)
I made a resource monitor for Raspberry Pi with a spreadsheet
I tried to automate [a certain task] using Raspberry Pi
getrpimodel: Recognize Raspberry Pi model (A, B, B +, B2, B3, etc) with python
I made a surveillance camera with my first Raspberry PI.
Creating a temperature / humidity monitor with Raspberry Pi (pigpio version)
A set of script files that do wordcloud in Python3
A program that receives the servo command of the radio control, interrupts the Raspberry Pi and logs it
Put the process to sleep for a certain period of time (seconds) or more in Python
A story that I had a hard time displaying graphs on top of each other with matplotlib