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.

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'
  • @AidenProhaska you might have to play with the +/- after -size and -mtime to get the right ones to print given your under/over 4k and before after 24hrs ago. I was not 100% how you wanted those. – Anthon Jun 17 '15 at 19:11
  • Good answer Anthon, -mtime or -newer find flags. – Baazigar Jun 17 '15 at 19:26
  • You may want to check this answer as well: Why does find -mtime +1 only return files older than 2 days?: I find -newermt 'yesterday' to be useful if your system supports it – steeldriver Jun 17 '15 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.