[Linux] How to remove specific files and directories

[Linux] How to remove specific files and directories

Goal

  • Remove old video, data, log files in my CCTV server


Intro

  • My CCTV server directory tree

CCTV_HOME
|
|__ bin
|__ cctv
|   |__ cam01
|   |   |__ data
|   |   |   |__ 201905
|   |   |   |__ 201906
|   |   |   |__ 201907
|   |   |   |   |__ 20190701
|   |   |   |   |__ 20190702
|   |   |   |   |__ ...
|   |   |   |   |__ 20190730
|   |   |   |   |__ 20190731
|   |   |   |   |   |   cctv_20190731001231123.avi
|   |   |   |   |   |   cctv_20190731001431145.avi
|   |   |   |   |   |   ...
|__ log
|   |   cctv_20190415212234567.log
|   |   cctv_20190613112834139.log
|   |   cctv_20190711082932345.log
|   |   ...
|__ tmp
|   |   temp_20190415212234567.dat
|   |   temp_20190613112834139.dat
|   |   temp_20190711082932345.dat
|   |   ...


Practice

  • script
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash

###########################################################################
#
# Remove old files including .avi, .dat, .ini, .log and temporary files
#
###########################################################################
# 1.0.0  2019-07-31  dorbae    Initialize
###########################################################################

# Home directory
MY_CAMERA_HOME=/home/dorbae/cctvhome
# Define how many days you keep the files
KEEP_DAYS=7

# Remove old temporary files which were modified more than a day ago
find ${MY_CAMERA_HOME}/tmp/ -name 'temp_*' -mtime +1 -exec rm -f {} \;

# Remove old log files which were modified more than n days ago
find ${MY_CAMERA_HOME}/log/ -name '*.log' -mtime +${KEEP_DAYS} -exec rm -f {} \;

# Remove old video and data files which were modified more than n days ago
find ${MY_CAMERA_HOME}/cctv/ -name 'cctv_*' -mtime +${KEEP_DAYS} -exec rm -f {} \;

# Remove old YYYYMMDD(daily) directory
# eg. if today is 2019-07-31, remove ${MY_CAMERA_HOME}/cctv/*/data/201907/20190724
OLD_DAILY_DIRECTORY=`date '+%Y%m%d' -d "${KEEP_DAYS} day ago"`
OLD_MONTH_DIRECTORY=`date '+%Y%m' -d "${KEEP_DAYS} day ago"`
rm -rf ${MY_CAMERA_HOME}/cctv/*/data/${OLD_MONTH_DIRECTORY}/${OLD_DAILY_DIRECTORY}

# Remove old YYYYMM(monthly) directory
# Also, remove directory which were created 2 month ago.
# Because I want to remove unnecessary directories certainly if it was not deleted accidently by any reason.

# - option : remove a padding(0)
# eg. %d -> 07, 08, 11 / %-d -> 7, 8, 11
TODAY=`date '+%-d'`
if [ ${TODAY} -gt ${KEEP_DAYS} ]; then
    MONTH_AGO_1_DIRECTORY=`date '+%Y%m' -d "1 month ago"`
    MONTH_AGO_1_YYYY=`date '+%Y' -d "1 month ago"`
    CURRENT_YYYY=`date '+%Y'`
    # I don't know why it works like that exactly
    # When I execute `date '+%Y%m' -d "1 month ago"` command on 31th July 
    # It returns not 06 but 07
    # I think it maybe depends on Timezone
    # So, check the month again manually
    if [ ${CURRENT_YYYY} -ne ${MONTH_AGO_1_YYYY} ]; then
        # Remove YYYYMMDD directory
        rm -rf ${MY_CAMERA_HOME}/cctv/*/data/${MONTH_AGO_1_DIRECTORY}
    fi
    
    # Remove the directory which were created 2 month ago
    MONTH_AGO_2_DIRECTORY=`date '+%Y%m' -d "2 month ago"`
    rm -rf ${MY_CAMERA_HOME}/cctv/*/data/${MONTH_AGO_2_DIRECTORY}
fi

exit 0

댓글남기기

-->