#!/bin/bash # Set the target size for camera folders in GB target_size_gb=90 # Directory containing camera folders base_dir="/media/disk1/video" # Найти и удалить пустые папки find "$base_dir" -type d -empty -delete # Iterate over camera folders for camera_dir in "$base_dir"/*/; do # Get the current size of the camera folder in GB without decimal part size_gb=$(du -s "$camera_dir" | awk '{printf "%.0f\n", $1 / 1024 / 1024}') # Calculate how much to delete to reach the target size space_to_free_gb=$((size_gb - target_size_gb)) # Check if the current size exceeds the target size if (( size_gb > target_size_gb )); then echo "Cleaning $camera_dir" # Delete oldest files until the folder size is reduced to the target size while (( space_to_free_gb > 0 )); do # Delete the oldest file in the folder find "$camera_dir" -type f -printf '%T@ %p\n' | sort -n | head -n 1 | cut -d' ' -f2- | xargs rm # Update the current size size_gb=$(du -s "$camera_dir" | awk '{printf "%.0f\n", $1 / 1024 / 1024}') # Calculate the remaining space to free space_to_free_gb=$((size_gb - target_size_gb)) done fi done