LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-02-2017, 03:55 PM   #1
Waris
Member
 
Registered: May 2017
Distribution: Centos 6,7 and Windows
Posts: 40

Rep: Reputation: Disabled
Bash script for how big file size is and different days of the week


Hello Everyone,


This is my first time posting to Linux questions, ever. Pleasure to be a part of the community. I am new to bash scripting and I've got a Bash scripting question i was wondering you all could assist me with me.

So my system generates log files on Monday-Friday. i have to write a script that tells me if the size is under a certain byte. in this case 1k. This is what i have so far.

file=date -d "yesterday" + %d%b%Y
minimumsize=1000
actualsize=$(wc –c <”$file”)
if [ $actualsize –ge $minimumsize ]; then
echo size is under $minimumsize bytes
else
echo size is $actualsize bytes
fi


These are the type of files i have
file1.txt
file2.txt
file3.txt
file4.txt

file-monday-1.txt
file-tuesday-2.txt
file-wednesday-3.txt
file-thirsday-4.txt

So because the files names gets generated how do tell it to check the sizes of all those files on different dates as they come? what am i missing from my script? Again i am very new to scripting in the first place your help would be appreciated.



Thanks in advance for the assistance.
 
Old 06-02-2017, 06:23 PM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
#!/bin/bash

yesterday=$(date -d yesterday +%A | tr '[:upper:]' '[:lower:]')

for i in *.txt
do

# show what we can tell about the file
ls -l "$i"
stat -t "$i"
    
# is it named after yesterday's day-of-week?
case "$i" in
*file-$yesterday-*.txt) echo DAY MATCH $i;;
*) echo ignoring $i;;
esac

echo

done
 
1 members found this post helpful.
Old 06-03-2017, 07:59 AM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
I must say that is a valent try with your script with the use of your variables.

Quote:
So because the files names gets generated how do tell it to check the sizes of all those files on different dates as they come?
you needed a loop and a means to tell it where to look and a means to get the files into that same loop to look at them and manipulate them to get the information out of them that you need. then to manipulate that into something you can work with.
Code:
#!/bin/bash

#for easy portability
#does not have to be ran in same dir as files.
working_dir=/home/userx/scripts/testing

#where to write the log and log name
log_dir=/home/userx/scripts/testing/FileSizes

while read file
do

max=1000
#get output from wc 
fileSize=$(wc -c $file)
#strip off excess
#from right to left
#find deliminator which is a space
#in this case
#cli reviles
#userx%slackwhere ⚡ testing ⚡> wc -c while-shuf
#795 while-shuf
#has a space between the number and the chars
# therefore that is a good
#pattern to look for and use
fileSize=${fileSize%% *}

#check size less than or equal to.
#for just less than is -lt
if [[ "$fileSize" -le "$max" ]] ; then
#spit it out to the screen
echo "Less than -> $file"
#write it to a log file
echo "$file : is $fileSize bytes" >> "$log_dir"
fi
done< <(find "$working_dir" -type f )
a snippet of results from the log file.
Quote:
/home/userx/scripts/testing/MapFile : is 392 bytes
/home/userx/scripts/testing/Fine8thFileToDelete : is 155 bytes
/home/userx/scripts/testing/findmatches : is 277 bytes
/home/userx/scripts/testing/Nested-While-Loop : is 192 bytes
/home/userx/scripts/testing/LongString : is 475 bytes
/home/userx/scripts/testing/casey : is 88 bytes
/home/userx/scripts/testing/testdata/File-May-4 : is 0 bytes
this checks every file in the directory and its subdirectories. as it states you are only looking for sizes,"of all those files on different dates"

it can be changed to just specif files ending with whatever before placed into the loop or within the loop itself. even kept within a certain directory within that branch. via find using -maxdepth and -mindepth

you can even add what you need off of @linosaurusroot script if need be to this one and have a polished script to impress your friends and strangers

to rewrite/over write the same log for each run ?? hum
Code:
  if [[ "$count" = '0' ]] ; then
  {
    echo "$file : is $fileSize bytes" > "$log_dir"
    ((count++))
   }
  else
    echo "$file : is $fileSize bytes" >> "$log_dir"
  fi
you just need to figure out where to put this block of code and where to initiate the variable used to get it to work.

Last edited by BW-userx; 06-03-2017 at 09:23 AM.
 
1 members found this post helpful.
Old 06-03-2017, 08:24 AM   #4
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,627

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
Do you really mean 1K or 1 thousand?

You set a variable to 1000 for the limit, but 1k is 1024.
Not that this affects any of the logic, I was just curious about your real intent.
 
Old 06-05-2017, 10:40 AM   #5
Waris
Member
 
Registered: May 2017
Distribution: Centos 6,7 and Windows
Posts: 40

Original Poster
Rep: Reputation: Disabled
Talking Thank you!

Thanks so much for the detailed reply. I am still very new to Bash scripting and scripting in general. I will give this a try and what see happens.







you just need to figure out where to put this block of code and where to initiate the variable used to get it to work.[/QUOTE]
 
Old 06-05-2017, 12:30 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Hi Waris and welcome to LQ.

I'm moving this discussion to Programming to give it some better exposure for this type of question.

For when you have updated code, please use [code][/code] tags around your code so that it will appear in code blocks like shown in the posts from linosaurusroot and BW-userx.

I would suggest at this time that you try to combine the concepts they have suggested and try to refine what the script does for you.

Additionally there are many references on the web, here are three for BASH scripting which may help you as you work on this project, or any future BASH projects:
Bash Guide for Beginners
Advanced BASH Scripting Guide
My Bash Blog
 
Old 06-05-2017, 04:46 PM   #7
Waris
Member
 
Registered: May 2017
Distribution: Centos 6,7 and Windows
Posts: 40

Original Poster
Rep: Reputation: Disabled
It worked guys.

Thanks so much for all the help!
 
Old 06-05-2017, 05:06 PM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
yes - glad it works for you - hope you pick it apart and learn from it.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
File size using BASH script cb951303 Programming 16 11-07-2012 07:45 AM
Bash script to remove files older than 3 days rust8y Linux - General 26 10-04-2012 08:26 AM
Basic bash script question re: file size or # of lines in a file the_fornicator Programming 6 09-03-2009 09:41 AM
Calculating age in days and month in a bash script jachba Programming 5 06-23-2006 01:37 PM
Bash script that returns the size of a file spank Linux - General 6 01-19-2006 03:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration