Created
May 21, 2024 23:43
-
-
Save delirehberi/d8a82a1ebf578c1c0d25c03a85a193cc to your computer and use it in GitHub Desktop.
Remover fo Old versions of deployments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check if a root folder parameter is provided | |
if [ $# -ne 1 ]; then | |
echo "Usage: $0 <root_folder>" | |
exit 1 | |
fi | |
root_folder="$1" | |
# Check if the provided root folder exists | |
if [ ! -d "$root_folder" ]; then | |
echo "Error: The specified root folder does not exist." | |
exit 1 | |
fi | |
# Find all folders in the specified root folder and sort them by modification time | |
folders=($(find "$root_folder" -maxdepth 1 -type d -printf "%T@ %p\n" | sort -n | awk '{print $2}')) | |
# Count the number of folders and set a threshold for the number to keep | |
num_folders="${#folders[@]}" | |
keep_count=5 | |
# Check if there are more folders than the threshold | |
if [ "$num_folders" -gt "$keep_count" ]; then | |
# Calculate how many folders to delete | |
delete_count=$((num_folders - keep_count)) | |
# Loop through and delete the old folders | |
for ((i = 0; i < delete_count; i++)); do | |
echo "Removing folder: ${folders[i]}" | |
rm -rf "${folders[i]}" | |
done | |
echo "Removed old versions" | |
else | |
echo "Nothing to remove." | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment