-
Mémo Garder les x derniers items dans un dossier
Keep the x last items in a folder
#!/bin/bash PATTERN="" ITEM_TYPE="" BASE_DIR="" KEEP=3 function show_help { echo -e 'Usage: ./clean-builds.sh [OPT]\nOptions:' echo -e '\td\tBase directory' echo -e '\tp\tFile / folder pattern: "*my-item*"' echo -e '\tt\tItem type:\n\t\t f file\n\t\t d directory' echo -e '\th\tshows this message and exit' exit } while getopts d:p:t:h OPTION; do case "${OPTION}" in d) BASE_DIR=${OPTARG} ;; p) PATTERN=${OPTARG} ;; t) ITEM_TYPE=${OPTARG} ;; h|*) show_help exit ;; esac done DIRS_COUNT=$(find "${BASE_DIR}" -maxdepth 1 -name "${PATTERN}" -type "${ITEM_TYPE}" | wc -l) shift "${DIRS_COUNT}" if [[ "${DIRS_COUNT}" -gt "${KEEP}" ]] then TO_DELETE="$(($DIRS_COUNT-3))" find "${BASE_DIR}" -maxdepth 1 -name "${PATTERN}" -type "${ITEM_TYPE}" | sort | head -n ${TO_DELETE} | xargs -I{} rm -rf {} echo "SUCCESS!" exit fi echo "NOTHING TO DO"
Usage:
$ ./clean.sh -d "/home/user/" -p "*db-backup.sql" -t "f"
30 décembre 2021 12:41