1

I have some old file in my Linux server and I need to remove those files which are older than 5 days based on the threshold value.

log_space_checker() {
        Use=$(df -kh /logs | awk 'END{gsub("%",""); print $4}');
        DATAUSE=$(df -kh /logs | awk 'END{gsub("%",""); print $4}');
}

remove_files(){
        RemoveFiles="/logs/abc/abc.log.*"
        find "$RemoveFiles" -mtime +1 -type f -delete
        }

disk_space_monitor() {
        log_space_checker;

        if [[ $DATAUSE -gt $TH ]] ; then
                remove_files;
        fi

}

TH=7

disk_space_monitor

Is the script correct?

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
shabaz
  • 13
  • 4
  • 2
    suggest you change the -delete to -print and review its behaviour. create files with old timestamps using touch if needed too. – steve Jul 26 '16 at 06:29
  • what is the use of Use variable here ? – Rahul Jul 26 '16 at 06:36
  • do you really have a directory called "/logs/abc/abc.log.*" (with star at the end of the name)? – Jasen Jul 26 '16 at 08:18
  • df -kh would ask for output both in kB and in "human readable" (variable units). Also the fourth column (counting from 1) of GNU coreutils df output is the available space, not the percentage. – ilkkachu Jul 26 '16 at 08:41
  • This is one of those times when you don't want to quote a variable. In remove_files() you have RemoveFiles="/logs/abc/abc.log.*". To expand that with a shell glob (so that the * matches as a wildcard rather than a literal asterisk) you must not quote it, so find $Remotefiles ... – Chris Davies Jul 26 '16 at 09:03
  • Not a duplicate. Notice the threshold determination that's also a requirement here – Chris Davies Jul 27 '16 at 21:58

1 Answers1

1

Me, I'd go for something like this

#!/bin/bash
#

########################################################################
#
log_space_checker() {
    local target="$1"                 # Log file template
    local directory="${target%/*}"    # Directory holding log files
    df -k "$directory" | awk 'NR>1 {print gensub("^.* ([1-9][0-9]*)%.*", "\\1", 1)}'
}

########################################################################
#
remove_files(){
    local logfiles="$1"               # Log file template
    find $logfiles -mtime +1 -type f -print ## -delete
}

########################################################################
#
disk_space_monitor() {
    local threshold="$1"              # Threshold%
    local target="$2"                 # Log files to delete 
    local pct_used=$(log_space_checker "$2");

    if [[ $pct_used -gt $threshold ]]
    then
        remove_files "$2"
    fi
}

########################################################################
# Go
#
threshold=7                           # % usage above which we will delete
logfiles='/logs/abc/abc.log.*'        # ...log files matching this pattern

disk_space_monitor "$threshold" "$logfiles"
Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • @Jasen thanks for your input but the script that you have provide has some error i am trying to fix it through the online tool.

    can you let me know do i need to give my current directory in place of $2 (local target="$2")

    i am a new learner so bit getting confused :)

    – shabaz Jul 28 '16 at 12:19
  • @shabaz, no bug, i should have looked closer. – Jasen Jul 30 '16 at 22:47
  • @roanma, no bug... I was misreading it. – Jasen Jul 30 '16 at 22:49