I have so much files and folders. They have a syntax like this path
/opt/logs/qnap/[hosta,hostb]/2012/03/12
All log file on the Qnap data storage and it is in the another location. So when I want to delete files & folders which are older than 180 days I cannot use find command cause of the slowness. So I wrote a script like this .
SixMonthAgo=$(date --date='190 day ago' "+%Y/%m/%d/%H") = 2011/06/12/12
Hosts="hosta hostb"
maxDay=181
qnapFolder="/opt/logs/qnap"
for host in $(echo "${Hosts}"); do
maxDayAgo=$(date --date="${maxDay} day ago" "+%Y/%m/%d")
countCharacters=$(echo ${maxDayAgo} | wc -c)
if [ ${countCharacters} -ge 10 ]; then
rm -rf ${qnapFolder}/${host}/${maxDayAgo} #output of this just like 2012/03/12
fi
done
But sometimes i got an error. This soluiton is not working correctly what I want. I want to delete all files and folders before 180 days. How can I do this ? That must delete before all files and directories before 2012/03/23 for example .
Thanks in advance
The find command is slow? Are you certain? The only drawback is that it fails to remove the directory structure.
find /opt/logs/qnap -type f -mtime +180 -deleteIf the directories were indeed created at the same time then maybe removing
-type fcould work for you.find /opt/logs/qnap -mtime +180 -deletefind is very fast mind you … but if you really want to do it with a shell script …