-2

I need to compare the size of the file generated today with the one of yesterday (everyday a new one is generated - huge in size)

fileA_20150716.log
fileB_20150717.log
fileC_20150718.log
fileD_20150719.log

What would be the best approach for this (new to coding)? I found this code but I think it is not good for leap years, etc.

prev_date=`TZ=bb24 date +%Y%m%d`
echo $prev_date

3 Answers3

0

You could start from this Bash script and expand it to your needs:

#!/bin/bash

A=(*.log)

for ((i = 0; i < ${#A[@]} - 1; i++)); do
  diff "${A[i]}" "${A[i + 1]}"
done

It does not use dates but rather compares adjacent pairs of log files. You can replace the diff command with something more suitable for you.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
0

I need to compare the size of the file generated today with the one of yesterday

Instead of constructing the filename for yesterday's file from today's date (which is tricky, as you found out), why not simply look for a file with the correct name (pattern) and a modification time of yesterday? That's much easier:

TODAYSSIZE=`find . -name "file*\.log" -daystart -mtime 0 -printf "%s"`
YESTERSIZE=`find . -name "file*\.log" -daystart -mtime 1 -printf "%s"`

Then do with the values whatever you need to do.

Tweak search path (.), file name pattern (file*\.log) and the actual size format displayed (%s) to your requirements.

This is assuming you have GNU find available; the find shipped with AIX doesn't do -printf. You can still use it to get the filenames, though:

TODAYSFILE=`find . -name "file*\.log" -daystart -mtime 0`
YESTERFILE=`find . -name "file*\.log" -daystart -mtime 1`

Then retrieve the file size any way you like (ls -s $TODAYSFILE or whatever).

Note that find works recursively, i.e. it would find logfiles in subdirectories as well. GNU find can be told to -maxdepth 1 to avoid this, AIX find cannot.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • I first encountered AIX seven years ago, when I inherited a company project that was supposed to run on AIX as well. And to this day, I haven't stopped hating that system's guts, not least because of its HODOR userspace tools... – DevSolar Jul 20 '15 at 14:39
  • Thanks for your interest in helping me but commands like -maxdepth -daystart are not available on the system. – Sergio M. S. Jul 20 '15 at 14:58
  • @SergioM.S.: Lack of `-maxdepth` is not a problem if you don't have (current) logfiles in subdirectories. Sorry for not cross-checking `-daystart` with AIX `find`, but it's a convenience function really: Depending on when your logfiles are last touched and when your script runs (I am assuming some kind of cron-job here), you don't need `-daystart` and can work with `-mtime` alone. Since you did not give all the relevant information, any answer can only point into a general direction, and I pointed towards `find` as alternative to filename tinkering. You will have to adapt it to your needs. – DevSolar Jul 20 '15 at 15:01
  • What I was thinking is, 1) Finding files ranging from 24 hours before current time (should return 1 file since this scripts runs once) 2) Store the filename found 3) Compare today file with file found through find command Do you think this will work? – Sergio M. S. Jul 20 '15 at 15:12
  • @SergioM.S.: There are many ways you could go about this. `ls -tr file*.log | tail -n 2` for example would give you *the last two* logfile names. Tinkering with the filenames is probably the most complicated and fragile way to do it. – DevSolar Jul 20 '15 at 15:17
  • Thanks, from the provided command (ls -tr file*.log | tail -n 2), do you know how I can store the result in two separate variables? or through an array? – Sergio M. S. Jul 20 '15 at 15:23
  • @SergioM.S.: You aren't really familiar with command line tools, or looking up man pages once pointed toward a useful tool, are you? `ls -tr file*.log | tail -n 2` gives the last two files, appending `| head -n 1` to that gives you the first of the two,`ls -tr file*.log | tail -n 1` would give you only the last file. – DevSolar Jul 20 '15 at 15:34
0
#!/usr/bin/bash

a=$(date  +%Y%m%d)
b=$(date -d yesterday  +%Y%m%d)

for f in *$a.log
do
    wc $f ; wc ${f/$a/$b}
done
JJoao
  • 4,891
  • 1
  • 18
  • 20