I had created a shell script, to find files that use highest disk space and files that are recently modified. Our disks are 2-3 TB in size and the script takes hours to complete. Can the script be optimized to reduce the execution time? Find command invokes the time in here. Is there any other efficient way to find the recently modified files?
#!/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