IT6004 - Unix Systems - Lab8

Finished Lab

Posted on 11/13/2024

#!/bin/bash

# Script name: findBigFiles.sh
# Author: Husain Alderazi
# Date: 13/11/2024
# Purpose: Find files larger than a specified size in the current directory

function usage {
    echo "***************************************"
    echo "USAGE: $SCRIPT_NAME [Number_Of_Meg_Bytes]"
    echo "EXAMPLE: $SCRIPT_NAME 5"
    echo "Will Find Files Larger Than 5 Mb in and below the Current Directory..."
    echo "EXITING..."
    echo "***************************************"
}

function trap_exit {
    echo -e "\n**********************************************"
    echo -e "\n\n EXITING ON A TRAPPED SIGNAL..."
    echo -e "\n\n**********************************************\n"
}

trap 'trap_exit; exit 2' 1 2 3 15

if [ $# -ne 1 ]; then
    usage
    exit 1
fi

DATESTAMP=$(date +"%h %d,%Y,%T")
SEARCH_PATH=$(pwd)

echo "Searching for Files Larger Than $1Mb starting in:"
echo "$SEARCH_PATH"
echo "Please Standby for the Search Results..."

find "$SEARCH_PATH" -type f -size +"$1"M -print > results.out

if [ ! -s results.out ]; then
    echo "No files were found that are larger than $1Mb"
    echo "Exiting..."
    exit 0
fi

num_files=$(wc -l < results.out)
echo "Number of files found: $num_files"
echo "Results are in the file: results.out"