Daily File Store Shell Script
Introduction
Simple shell scripts that store pictures into folders every day and clean it up later on if you wish.
Detailed Description
This is my script that is called from cron at one minute before midnight with a cron line like this:
59 23 * * * /root/midnight.sh >/dev/null 2>&1
The first script is called "midnight.sh", don't forget to set it's permissions to 755 (use chmod 755).
The other script below is called "cleanup-dirs.sh", you can use it if you like, it's called at the end of the other script and will delete all old files and remove the directory, SO BE CAREFUL TO GET THIS ONE RIGHT. You don't have to use this, but it's nice to clean up all those old files. It has handled days with 40,000 files with my setup.
My setup stores the pictures at "/root/pic-link", you need to change this to what your setup has. In my case "pic-link" is a symbolic link to a /home directory I use to store pictures because the /home directory is the largest directory. You may run out of room if you just use the root directory (I operate everything root on my home remote system on my private internal network, it's up to you).
The number in the script below, "7776000" is 90 days in seconds, you can change that to how old the directories should be to be removed.
The script below is the one called "midnight.sh":
#! /bin/sh
# at midnight we move everything into it's own dated directory
TEMP=`date +%F`
cd /root/pic-link
mkdir $TEMP
ls -1 | grep jpg | xargs -n 10 -i mv {} $TEMP/
rm -f $TEMP/lastsnap.jpg
cd /root
# now remove old directories and files
./cleanup-dirs.sh
The script below is the one called "cleanup-dirs.sh":
#! /bin/sh
cd /root/pic-link
for i in *
do
if [ -d $i ]
then
TODAY=`date +%s`
FILETIME=`date +%s -d $i`
if [ $? -eq 0 ]
then
DIFF=`expr $TODAY - $FILETIME`
if [ $DIFF -gt 7776000 ]
then
echo $i $DIFF "more than 90 days, say bye bye"
cd $i
# remove a lot of files, then remove dir
ls -1 | xargs -n 10 -i rm -f {}
cd ..
rmdir $i
else
echo $i $DIFF "OK"
fi
else
echo $i "is not a dated directory"
fi
fi
done