Sh script conditional for file size?

software development

#1

What’s the syntax for a sh script conditional to check that a file’s size is greater than 1 kb?


#2

I don’t think there’s a direct way to do this - you might check the CONDITIONAL EXPRESSIONS section of the bash man page for some basic building blocks… you could use the “test” / “[” builtins along with checking the output of du or ls.


#3

A little more background on what I’m doing: I have some scripts running samba tar backups on some PC shares in a cron. If the PC happens to be off, samba still creates a small tar, usually 33 bytes, but always less than 1000 bytes. Therefore I can’t test for the tar file’s existence or file size = 0. I’d like to make sure that the file is greater than about 1000 bytes before echoing the backup worked date >> logfile.

And yes, I looked at a refernce of basic expressions and I was surprized that I didn’t see a simple way to do it.


#4

Right - but you could /first/ test that the file exists and is > 0; then after that, you can use “du” or “ls” to find the file’s size and check that. So I don’t think there’s a way to do what you want without a little work, but all the building blocks you need are there. If I get some time, I’ll try to think about how I’d approach this problem… my shell scripting skills aren’t as good as they perhaps should be.