用于查找最近修改的文件的Shell脚本

时间:2017-10-18 07:46:09

标签: linux bash shell

我创建了一个shell脚本,用于查找使用最高磁盘空间的文件和最近修改过的文件。我们的磁盘大小为2-3 TB,脚本需要数小时才能完成。可以优化脚本以减少执行时间吗?查找命令在此处调用时间。有没有其他有效的方法来查找最近修改过的文件?

#!/bin/bash

outfile="/tmp/output.txt"

function printline() {
echo "-------------------------------------------------------------" | tee -a $outfile
echo "" | tee -a $outfile
}

function nullcheck() {
echo ""
echo "Usage: search.sh [directory path]"
echo ""
}

if [ $# -ne 0 ];then
        if [ -d $1 ];then
                echo ""
                printline;
                echo $(hostname) $(date) "user:"$(whoami) | tee -a $outfile
                echo "" | tee -a $outfile
                echo "---------------------Current Disk Usage----------------------" | tee -a $outfile
                df -hP $1 | tee -a $outfile
                printline;
                LARGE=$(find $(readlink -f $1) -type f -exec du -Sh {} \+ | sort -rh | head -50)
                echo "--------------------Largest top 50 files---------------------" | tee -a $outfile
                echo "$LARGE" | tee -a $outfile
                printline;
                echo "------Newly created/modified Files for the last 6 hours------" | tee -a $outfile
                FILES=$(find $(readlink -f $1) -type f -mmin -360 -exec ls -ltrh {} \+)    ##find all the files that are modified in last 6 hours
                if [ -z "$FILES" ];then
                        echo "None of the files are created/modified during this time period"
                else
                        echo "$FILES" | tee -a $outfile
                fi
                printline;
        else
                echo "Directory doesn't exist" | tee -a $outfile
        fi
else
        nullcheck;
fi

0 个答案:

没有答案