Answer the question
In order to leave comments, you need to log in
Smart cleaning of old archives?
Good afternoon. We need help in organizing the cleanup of the code deployment archives directory. There is a directory /var/www/archive . It has 4 builds. Let's call them ABC D. I need to clean this directory from assemblies more than 3 versions. That is, for example, there are assemblies A.1.tar.gz-A10.tar.gz If assemblies ABC are updated frequently, then assemblies D are updated once every 3 months, or even less often. How to do smart cleaning. Let's check how many assembly versions there are, if there are more than 3 then delete the oldest ones, and leave the last 3. Thanks
Answer the question
In order to leave comments, you need to log in
It took a long time to google and figure it out.
#!/bin/bash
build="A_*.tar.gz"
build2="B_*.tar.gz"
build3="C_*.tar.gz"
build4="D_*.tar.gz"
dirb="/var/www/archive"
for project in $build $build2 $build3 $build4
do
count=`ls $dirb/$project | wc -l`
if [ $count -gt 3 ]; then
ls -1t $dirb/$project | sort -h -r | tail -n +4 | xargs rm
fi
done
it will be easier to put them in different directories
and run different
find -mtime -delete through these directories
Script in cron for every "day"
#!bin/bash
count=`ls Ваш_каталог/ |wc|awk '{print $1}'`
if [ $count -gt 3 ]
then
tr=`expr $count - 3`
files=`ls Ваш_каталог/ -tr |head -$tr`
cd Ваш_каталог/ && rm -rf $files
printf "Ох как много файлов!АЖ $count, значит удаляю следующие файлы \n$files\n"
else
printf "Все ок, количество файлов: $count \n"
fi
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question