Sunday, July 12, 2015

Unix Shell Script for Viewing Size of Specific Files as well as with Addition of Similar Files



echo "This script is to show the size of files as well as collectively addition of similar file for the today and yesterday timestamp."

#Goto the folder where files are located
cd <destinated_folder_structure>

#Below command will list the files with the matching pattern then it will grep for the yesterday date file and size of file is storing in the temp file. Later in second line it doing the addition which will be overrite the temp file.
ls -ltrh <FileMatchingPattern>*txt* | grep "$(date --date="yesterday" '+%b %e')" | awk '{print $6, $7, $9, $5}' > y1.txt
ls -ltrh <FileMatchingPattern>*txt* | grep "$(date --date="yesterday" '+%b %e')" | awk '{total += $5}; END {print total}' >> y1.txt

#Similar activity as first command is doing but it will do for the Today's date
ls -ltrh <FileMatchingPattern>*txt* | grep "$(date '+%b %e')" | awk '{print $6, $7, $9, $5}' > t1.txt
ls -ltrh <FileMatchingPattern>*txt* | grep "$(date '+%b %e')" | awk '{total += $5}; END {print total}'>> t1.txt

#If you have more than one file pattern then you show them collectively. Let's assume below is another pattern and want to show it.

ls -ltrh <FileMatchingPattern>*txt* | grep "$(date --date="yesterday" '+%b %e')" | awk '{print $6, $7, $9, $5}' > y2.txt
ls -ltrh <FileMatchingPattern>*txt* | grep "$(date --date="yesterday" '+%b %e')" | awk '{total += $5}; END {print total}'>> y2.txt

ls -ltrh <FileMatchingPattern>*txt* | grep "$(date '+%b %e')" | awk '{print $6, $7, $9, $5}' > t2.txt
ls -ltrh <FileMatchingPattern>*txt* | grep "$(date '+%b %e')" | awk '{total += $5}; END {print total}'>> t2.txt


cat y1.txt > final.txt
cat t1.txt >> final.txt
cat y2.txt >> final.txt
cat t2.txt >> final.txt

#So if you want to see on the screen

cat final.txt

#If you want to mail the results

mail -s "Today and Yesterday File Size" abc@mail.com -c xyz@mail.com < final.txt

#If you wish you can delete the temporary generated file

rm -f y1.txt t1.txt y2.txt t2.txt final.txt


Save this file with .sh or .ksh extension and make it shell script and be run by ./<filename>.sh

After saving this file you may have to give permission to run so execute the below command
chmod 755 <filename>.sh

That's it.

Please leave your comments.

Thanks.

1 comment:

  1. Great stuff provided by the Admin here… look into this for Obiee online training

    ReplyDelete