The FTP service to transfer the photoes of our laboratories every minitue has been deployed last week, but my server's capacity only has one TB composed of two 500 GB hard disks. So these hard disks are probable to be full in future. An idea attacked me that I can code a script to remove these photoes captured one monthes ago automatically.

#!/bin/bash
#To delete files once a certain time
# lixin@xs.ustb.edu.cn
ctime=$(date +%Y-%m-%d --date="-1 month")
for i in /home/dell/video/*
do 
    d1time=$(ls $i --full-time | awk '{print $6}');
    if [ "$d1time" == "$ctime" ];then 
        rm $i
        echo $i >> /home/dell/log_video209
    fi
done
echo $ctime >> /home/dell/log_video209
for i in /home/dell/video111/*
do 
    d2time=$(ls $i --full-time | awk '{print $6}');
    if [ "$d2time" == "$ctime" ];then 
        rm $i
        echo $i >> /home/dell/log_video111
    fi
done
echo $ctime >> /home/dell/log_video111
# How to set a plan to execute this script?
# root user:
# edit this file "/etc/crontab" in notepad(vim,nano,emacs)
# reference : crontab
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
# 17 *  * * *   root    cd / && run-parts --report /etc/cron.hourly
# 25 6  * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
# 47 6  * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
# 52 6  1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
# If you want to execute this script every day:
# you can type this:
#  0 0 * * *  root    /home/dell/deletefiles.sh
# reboot is not needed for crontab, it will check crontab every minitues.
# you can use command "grep CRON /var/log/syslog" to check log.

Error:

Syntax error: unexpected end of file

Solution:

vi deletefiles.sh
:set fileformat=unix
:w

Error2:

[: XXXX: unexpected operator

Soultion:

sudo dpkg-reconfigure dash

Choose the button "No" to switch dash with bash.

NOTICE
The path in the script must be a absolute path, otherwise it will find a wrong path for crontab.

Last modified: 2020年3月30日

Comments

Write a Reply or Comment

Your email address will not be published.