Click to See Complete Forum and Search --> : Working with BASH and file sizes..
eLamer2
10-10-2002, 10:01 AM
I'm wondering how, with BASH, I could do an if-else statment with the condition being the filesize of a given file. What I want to do is check if a file is above X mb, and if so, add 1 to a variable stored in a text file. The point of this is I am writing an automatic network backup script and I am using that number from the text file as a variable for the filename. The file will be backuparchive"$variable".zip or whatever. Therefore, when the first file reaches 600 mb, the variable will change and when it is loaded at the beginning of the script from the text file it will cause a new zip file to be created. Any ideas?
demian
10-10-2002, 10:21 AM
This might be a little clumsy but you could use find /path/to/file -size +614400k The output will be empty if file is smaller than 600MB or else the filename so I guess this will work as input for an if-test.
TheLinuxDuck
10-10-2002, 10:42 AM
Try this out:
#!/bin/bash
FILE="/usr/bin/gs"
SIZE=`ls -l $FILE | tr -s ' ' | cut -f 5 -d ' '`
echo "Size of '$FILE' is $SIZE"
if [ "$SIZE" -ge "1024000" ]; then
echo "It is bigger than 1 meg"
fi
exit
Works like a champ. (=
eLamer2
10-10-2002, 02:57 PM
thanks alot, i'm gonna go work on it...
eLamer2
10-10-2002, 04:25 PM
Ok so now I have a variable -
FILENAME=/path/path/textfile
The textfile only has a number in it so I assume that that example will work, hopefully, tell me how to set the contents of the textfile to a variable if I'm wrong, ;) ANYWAY, what I want to do is with the IF command with the file size condition, how to write to the file.. If I just did
$FILENAME + 1; or something, would it change the contents of the textfile?
If not, how do I?
X_console
10-11-2002, 02:59 AM
No that would not work. You need to read the contents of the file, increment that by 1 and then store it back in the file. Something like this:
# get the number from FILENAME and store it in a variable:
fileContent=$(cat $FILENAME)
# increment the variable by 1:
fileContent=$(($fileContent + 1))
# write the variable to the file, and destroy the current contents:
echo $fileContent > $FILENAME
Edit: Fixed a typo in the code.
eLamer2
10-11-2002, 11:38 AM
ok thanks, i'll work on it and come back with what i come up with.. i would test my examples but i am doing this at school, lol
bigrigdriver
10-15-2002, 04:52 AM
Don't know if you've heard of it, but you might want to do a web search for and download a copy of the "Bash Reference Manual". Lots of good reading and examples there.