I have a little problem. Every time I deploy a release, I move the old one into an old-folder. The problem is: is there a way to delete all files in this old-folder except the 3 last files? kind of an opposite of ls -tr | tail -n 3
?
I took half an hour and hacked a little script the easy and st00pid way:
#!/bin/bash
#
# Name: del-except-3
# Author: pes
# Date: 08.04.2010
# Description
# Deletes all files in ${searchpath} except the last 3
#
debug=1
searchpath=$1
if [ ! -d ${searchpath} ]; then
echo "${searchpath} does not exist"
exit 1
fi
cd ${searchpath}
last3=`ls -tr | tail -3`
set -- ${last3}
last3_1=$1
last3_2=$2
last3_3=$3
if [ ${debug} -ne 0 ]; then
echo "last 3 files in ${searchpath}"
echo " 1: $last3_1"
echo " 2: $last3_2"
echo " 3: $last3_3"
fi
for i in `ls -1`; do
if [ "${i}" != "${last3_1}" ]; then
if [ "${i}" != "${last3_2}" ]; then
if [ "${i}" != "${last3_3}" ]; then
echo "rm ${searchpath}/${i}"
rm ${i}
fi
fi
fi
done
is there a smart combination of shell commands? someting that does not look that stupid?
Das liefert dir alle Files ausser die letzten drei:
ls -tr | head -n $((`ls | wc -l`-3))
Den Rest musst du selber machen 😉
IMPROVEMENT! 😀
cool, danke! also an head -n hab ich schon gedacht, allerdings nicht an die berechnung der gesamtanzahl der dateien minus die gewünschten drei 😀 ph4t!
ls -tr | head -n $((`ls | wc -l`-2)) is t3h r0x0r.