From 556c121ebb8b2c3645b74328ce08c64221f2f146 Mon Sep 17 00:00:00 2001 From: Bruno21 Date: Thu, 11 Apr 2024 14:51:19 +0200 Subject: [PATCH] First commit Clean Lightroom backup folders --- lightroom.command | 80 +++++++++++++++++++++++++++++++++++++++++++++++ lightroom.sh | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100755 lightroom.command create mode 100755 lightroom.sh diff --git a/lightroom.command b/lightroom.command new file mode 100755 index 0000000..3e01e7f --- /dev/null +++ b/lightroom.command @@ -0,0 +1,80 @@ +#!/usr/bin/env bash + +italic="\033[3m" +underline="\033[4m" +ita_under="\033[3;4m" +bgd="\033[1;4;31m" +red="\033[1;31m" +bold="\033[1m" +box="\033[1;41m" +reset="\033[0m" + +# No distract mode (no user interaction) +[[ $@ =~ "-nodistract" || $@ =~ "-n" ]] && no_distract=true || no_distract=false + +command -v fd >/dev/null 2>&1 || { echo -e "${bold}Clean Lightroom backups${reset} require ${bold}fd${reset} but it's not installed.\nRun ${italic}(brew install fd)${reset}\nAborting..." >&2; exit 1; } + +echo -e "${bold}+++ Clean Lightroom backups +++${reset}\n" + +echo -e "${underline}Preferences:${reset}" + +# Dossiers des catalogues Lightroom +lightroom=/Users/bruno/Pictures/Lightroom +echo "Catalog's folder: $lightroom" + +# Liste des catalogues Lightroom +catalogs=$(fd -I -t file '^.*.lrcat$' $lightroom) + +# Laisser n backup +n=2 +echo -e "Keep $n backups..." + +echo -e "Backups with extra names like '2022-01-20 0926 MàJ 2021' won't be deleted ...\n" + +while IFS= read -r line; do + l="${line}" + + catalog=$(basename "$l") + echo -e "${bold}$catalog${reset}" + folder=$(dirname "$l") + + if [ -d "$folder/Backups" ]; then + + backup=$(ls -t "$folder/Backups" | grep -v '[[:alpha:]]') + echo -e "${underline}Availables backup:${reset}" + echo "$backup" + nb=$(echo "$backup" | wc -l) + trash=$(echo "$backup" | tail -n $((nb-$n))) + + if [ -n "$trash" ]; then + while IFS= read -r z; do + + x="$folder/Backups/${z}" + + if [ -d "$x" ]; then + + if [ $(ls -A "$x" | wc -l) -ne 0 ]; then + + if [ "$no_distract" = false ]; then + echo -e "${italic}Do you want to delete $x (y/n)?${reset}" + read -u 1 answer + if [[ $answer = y ]]; then + rm -r "$x" + fi + else + echo -e "${italic}Deleting $x ...${reset}" + rm -r "$x" + fi + fi + fi + + done <<< "$trash" + + else + echo "No backup folder to delete !" + fi + + echo "" + fi + +done <<< "$catalogs" diff --git a/lightroom.sh b/lightroom.sh new file mode 100755 index 0000000..3e01e7f --- /dev/null +++ b/lightroom.sh @@ -0,0 +1,80 @@ +#!/usr/bin/env bash + +italic="\033[3m" +underline="\033[4m" +ita_under="\033[3;4m" +bgd="\033[1;4;31m" +red="\033[1;31m" +bold="\033[1m" +box="\033[1;41m" +reset="\033[0m" + +# No distract mode (no user interaction) +[[ $@ =~ "-nodistract" || $@ =~ "-n" ]] && no_distract=true || no_distract=false + +command -v fd >/dev/null 2>&1 || { echo -e "${bold}Clean Lightroom backups${reset} require ${bold}fd${reset} but it's not installed.\nRun ${italic}(brew install fd)${reset}\nAborting..." >&2; exit 1; } + +echo -e "${bold}+++ Clean Lightroom backups +++${reset}\n" + +echo -e "${underline}Preferences:${reset}" + +# Dossiers des catalogues Lightroom +lightroom=/Users/bruno/Pictures/Lightroom +echo "Catalog's folder: $lightroom" + +# Liste des catalogues Lightroom +catalogs=$(fd -I -t file '^.*.lrcat$' $lightroom) + +# Laisser n backup +n=2 +echo -e "Keep $n backups..." + +echo -e "Backups with extra names like '2022-01-20 0926 MàJ 2021' won't be deleted ...\n" + +while IFS= read -r line; do + l="${line}" + + catalog=$(basename "$l") + echo -e "${bold}$catalog${reset}" + folder=$(dirname "$l") + + if [ -d "$folder/Backups" ]; then + + backup=$(ls -t "$folder/Backups" | grep -v '[[:alpha:]]') + echo -e "${underline}Availables backup:${reset}" + echo "$backup" + nb=$(echo "$backup" | wc -l) + trash=$(echo "$backup" | tail -n $((nb-$n))) + + if [ -n "$trash" ]; then + while IFS= read -r z; do + + x="$folder/Backups/${z}" + + if [ -d "$x" ]; then + + if [ $(ls -A "$x" | wc -l) -ne 0 ]; then + + if [ "$no_distract" = false ]; then + echo -e "${italic}Do you want to delete $x (y/n)?${reset}" + read -u 1 answer + if [[ $answer = y ]]; then + rm -r "$x" + fi + else + echo -e "${italic}Deleting $x ...${reset}" + rm -r "$x" + fi + fi + fi + + done <<< "$trash" + + else + echo "No backup folder to delete !" + fi + + echo "" + fi + +done <<< "$catalogs"