1

I'm writing a script to stat all files in a directory, and then find specific files that were created yesterday and tell me if they are larger than a certain size or not.

So basically I need this to find that file, which is a .gz type file (and should have been last modified at least 24 hours ago) and have it check its size to make sure it's not under a certain number, say 4 kilobytes for example. And if it is, under that 4 kilobyte size, it needs to echo "Failure" or something like that, and if it was above that threshold, it needs to echo "Success" or something similar.

Eventually I'll have it send an email if it was a fail or success.

So far I have it stating all the files and finding the ones that have been modified in the last 24 hours, but since I'm such a newby, I'm lost at this point:

for file in /*; do
  stat $file
done
find /* -mtime -1 -print

I have this running my whole computer right now, but it will eventually be ran on a specific directory.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

1 Answers1

3

You can use find for all of that including the size (man find):

 -size n[cwbkMG]
          File uses n units of space.  The following suffixes can be used:
          `b'    for  512-byte blocks (this is the default if no suffix is used)
          `c'    for bytes
          `w'    for two-byte words
          `k'    for Kilobytes (units of 1024 bytes)
          `M'    for Megabytes (units of 1048576 bytes)
          `G'    for Gigabytes (units of 1073741824 bytes)

So you can do:

find . -name "*.gz" -size -4k -mtime -1 -printf 'Failure %p\n'
find . -name "*.gz" -size -4k -mtime +1 -printf 'Success %p\n'
Anthon
  • 79,293