1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| #!/bin/bash
# declaration des variable
option="$1"
repertoire="$2"
tailledemande=$3
#la fonction qui calculerla taille
calcul_taille(){
total=0
taille_bloc=1024
repertoire=$1
option=$2
if [ $option == "-l" ] # "-l pour calculé la taille logique"
then
for f in "$repertoire"/*
do
if [ -f "$f" ]
then
t_f=$(du -b $f | cut -f 1)
let total=($total+$t_f)
fi
done
else
nb_bloc=$(du -s "$repertoire" | cut -f 1) # sinon on calcul la taille en bloc
let total=$nb_bloc*$taille_bloc
fi
echo $total
return 0
}
#fonction qui affiche le plus ancien fichier utilisé
calcul_fichier(){
min=$(date +%s)
file=""
repertoire=$1
for f in "$repertoire"/*
do
if [ -f "$f" ]
then
date_f=$(stat -c "%X" "$f")
if [ $date_f -lt $min ]
then
min=$date_f
file="$f"
fi
fi
done
echo $f
}
#test si le premier parametre est bien une option -l ou -b
if [ "$option" != "-l" -a "$option" != "-b" ]
then
echo "option usage: $0 <option> <repertoire> <tailledemande>"
exit 1
fi
#test si le deuxieme parametre est un repertoire
if ! [ -d "$repertoire" ]
then
echo "directory usage: $0 <option> <repertoire> <tailledemande>"
exit 2
fi
#test si le troisieme parametre est une taille de type entier
if ! [[ $tailledemande = +([1-9])*([0-9]) ]]
then
echo "size usage: $0 <option> <repertoire> <tailledemande>"
exit 3
fi
#la boucle qui permet de calculer la taille du repertoire en fonction de l'option saisie et de supprimer les fichiers anciens tant que la taille du repertoire est superieur à la taille demande
taille_repertoire=$(calcul_taille "$repertoire" "$option")
echo "Avant : $taille_repertoire octets"
let taille_voulue=$taille_repertoire-$tailledemande
taille_repertoire=$(calcul_taille "$repertoire" "$option")
while [ $taille_repertoire -gt $taille_voulue ]
do
fichier=$(calcul_fichier "$repertoire")
if [ -f "$fichier" ]
then
echo "fichier le plus ancien $fichier supprimé"
rm "$fichier"
taille_repertoire=$(calcul_taille "$repertoire" "$option")
fi
done
echo "Après : $taille_repertoire octets"
exit 0 |
Partager