Код: Выделить всё
#!/bin/bash
# Define the parent folder
PARENT_FOLDER="/backup"
# Check if the folder exists
if [ ! -d "$PARENT_FOLDER" ]; then
echo "Error: Folder $PARENT_FOLDER does not exist."
exit 1
fi
# Find and sort the subfolders by modification time (oldest first)
folders=($(find "$PARENT_FOLDER" -mindepth 1 -maxdepth 1 -type d -printf "%T+ %p\n" | sort | awk '{print $2}'))
# Check if there are at least two folders to delete
if [ "${#folders[@]}" -lt 2 ]; then
echo "There are less than two folders. No deletion will occur."
exit 0
fi
# Delete the two oldest folders
for i in {0..1}; do
echo "Deleting folder: ${folders[$i]}"
rm -rf "${folders[$i]}"
done
echo "Two oldest folders deleted successfully."
С уважением
Подробнее здесь: https://stackoverflow.com/questions/792 ... 2-old-ones