[SOLVED] Bash script for how big file size is and different days of the week
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
#!/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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.