K
K
Ka4a2015-11-06 11:59:07
linux
Ka4a, 2015-11-06 11:59:07

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

3 answer(s)
K
Ka4a, 2015-11-06
@ka4a

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

A
Andrew, 2015-11-06
@drevil

it will be easier to put them in different directories
and run different
find -mtime -delete through these directories

M
mars natsuhiboshi, 2015-11-06
@mars_unique

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

No fantasy after all

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question