We have several monitors in our laboratories, and they send images to our host to backup these vital data. But there are only 1440 images to store everyday because it was set to transfer data every minute. Absolutely, this configuration is insufficient for us to monitor labs, so we want to reduce the interval of transferring information(e.g. 2 seconds). However, there will be 43,200 images to store, and the directory will be pretty chaos. So the process of loading will be very slow when we open the directory. An idea flashed to my mind is that we can ask host to collate these photos automatically and create different folders to store these images. And the operate to delete data in these folders at regular intervals will be easier. So this script is written by me to realize these tasks above.
#!/bin/bash
# crrs.sh
# GNU General Public License v3.0
# Collate file directories automatically and remove old folders to release space
# lixin@xs.ustb.edu.cn
# University of Science and Technology Beijing
# Github: lixin555
echo "Start to analyse the information of directories."
datetype=()
num=0
maxn=$(ls -l /home/dell/video/ |grep "^-"|wc -l)
for i in /home/dell/video/*
do
((num++))
a=$(ls $i -l | awk '{print $1}')""
if [ ${a:0:1} == '-' ];then
dltime=$(ls $i --full-time | awk '{print $6}')""
isexist='n'
for element in ${datetype[@]}
do
if [ $dltime == $element ];then
isexist='y'
break
fi
done
fi
if [ $isexist == 'n' ];then
datetype=(${datetype[@]} $dltime)
fi
echo "Finished: $num, Total: $maxn."
done
echo "Done!!! Next Step~"
echo "Start to create directories."
#create directories
for i in ${datetype[@]}
do
mkdir /home/dell/video/$i
done
echo "Done!!! Next Step~"
echo "Start to transfer files to new folders."
#move files into directories
for i in ${datetype[@]}
do
for j in /home/dell/video/*
do
a=$(ls $j -l | awk '{print $1}')""
if [ ${a:0:1} == '-' ];then
dltime=$(ls $j --full-time | awk '{print $6}')""
if [ "$i" == "$dltime" ];then
mv -f $j /home/dell/video/$i/
fi
fi
done
echo "move $i"
done
echo "Done!!! Next Step~"
#delete folders
echo "Start to delete folders."
ctime=$(date +%Y-%m-%d --date="-1 month")
for i in /home/dell/video/*
do
if [ "$i" == "/home/dell/video/$ctime" ];then
rm -rf $i
echo $i >> /home/dell/log_videotest
fi
done
echo "All Done!!!"
Repository of Github: https://github.com/lixin555/crrs
Comments