This commit is contained in:
xyj 2023-12-11 11:05:05 +08:00
parent 7a126b3e23
commit 607451d783
2 changed files with 23 additions and 17 deletions

View File

@ -1,17 +0,0 @@
#!/bin/bash
set -e
# 指定文件夹路径
folder="/home/pi/mp4"
# 进入文件夹
cd $folder
# 获取文件夹的大小
size=$(du -sh "$folder" | awk '{print int($1)}')
while [ $size -gt 50 ]; do
# 获取最早创建的文件
oldest_file=$(ls $folder -tr | head -1 | xargs)
rm -f $oldest_file
size=$(du -sh "$folder" | awk '{print int($1)}')
if [ $size -le 50 ]; then
exit 0
fi
done

23
delete_than20G.py Normal file
View File

@ -0,0 +1,23 @@
import os
import glob
import time
# 设置您需要监控的文件夹路径
folder_path = '/home/lihai/pi/mp4'
# 删除的达到大小 20G 就一直删除
size = 20
# 检查文件夹大小如果超过20G则开始删除文件
# 获取当前文件夹的大小
while True:
folder_size = sum(os.path.getsize(f) for f in glob.glob(os.path.join(folder_path, '*')))
if folder_size > size * 1024 * 1024 * 1024: # 检查是否超过20G
# 获取所有文件的时间戳和文件名
files_info = [(os.path.getmtime(os.path.join(folder_path, f)), f) for f in os.listdir(folder_path)]
# 按时间戳排序,取最旧的文件
oldest_file = min(files_info, key=lambda x: x[0])[1]
# 删除最旧的文件
os.remove(os.path.join(folder_path, oldest_file))
else:
break
# 等待5秒钟
time.sleep(5)