script to compare latest two files using "diff" command.

Hello All,

On suse linux, I am having below files in one same folder and from that all files I want to use “diff” command on latest 2 files and want to save output of “diff” command in one separate file and then want to send that file through mail as attachment. I want to do this on weekly basis.
could you please help me on this.
Thanks in advance… :slight_smile:

-rw-r–r–. 1 root root 4.2K Aug 27 00:22 10.10.10.10_2015_08_27.cfg
-rw-r–r–. 1 root root 124 Sep 3 00:23 10.10.10.10_2015_09_03.cfg
-rw-r–r–. 1 root root 124 Sep 4 00:00 10.10.10.10_2015_09_04.cfg
-rw-r–r–. 1 root root 124 Sep 5 00:00 10.10.10.10_2015_09_05.cfg
-rw-r–r–. 1 root root 124 Sep 6 00:00 10.10.10.10_2015_09_06.cfg
-rw-r–r–. 1 root root 124 Sep 7 00:00 10.10.10.10_2015_09_07.cfg
-rw-r–r–. 1 root root 124 Sep 8 00:00 10.10.10.10_2015_09_08.cfg
-rw-r–r–. 1 root root 124 Sep 9 00:00 10.10.10.10_2015_09_09.cfg
-rw-r–r–. 1 root root 124 Sep 15 01:47 10.10.10.10_2015_09_11.cfg

Hi Santosh_2411,

could you please help me on this.

what have you tried so far, what problems do you face? Sounds like you just need the proper combination of “ls”, “diff” and “mail”…

Regards,
Jens

It may also be worth ensuring that you understand what defines the “latest
2 files”. Is that by file name, or date? Modification date, or access
date? Something else? It’s easy to assume that the modification dates
and file names will be the same in your case, but what if they are not for
some reason?

It may also be worthwhile to explain how this overly-simplified example
fits into a bigger business case. Without that, it sounds like a homework
problem (a trivial exercise without any actual value in the real world
designed to test very specific skills) when a much better solution may be
to use a backup solution that can grab config (or other) files and, when
you request them, show you a difference; this way you’re not filling up a
mail server with e-mail you never read. It also means you’re not abusing
a mail system as a backup environment when that is not what it is
(especially if it lacks its own backups). E-mail is easy to abuse,
though, so it gets used in a lot of these cases without considering the
full business case and impact of the solution.

Perhaps you are trying to watch for unauthorized modifications of files;
in that case, you may want something like Tripwire, or you may want to
enable filesystem auditing. Doing a backup of a configuration file on a
weekly basis to look for changes seems like a long time to look for
unauthorized changes, but without the business case it’s hard to know if
that was a value just tossed out or what.


Good luck.

If you find this post helpful and are logged into the web interface,
show your appreciation and click on the star below…

Hello Jens,
we are using these two commands for to picking up two latest files as below:

latest=ls -lt /opt/soc/log | tail -n +2 | head -1
old=ls -lt /opt/soc/log | tail -n +3 | head -1

But we are not able to compare & redirect the output of this above two variables to a new file.

diff <($(echo $latest)) <($(echo $old)) > new_file.txt

Also we have tried below command too.
diff <(echo $latest) <(echo $old) > new_file.txt

Thanks for your concern.

Hi Santosh_2411,

[QUOTE=Santosh_2411;29626]Hello Jens,
we are using these two commands for to picking up two latest files as below:

latest=ls -lt /opt/soc/log | tail -n +2 | head -1
old=ls -lt /opt/soc/log | tail -n +3 | head -1

But we are not able to compare & redirect the output of this above two variables to a new file.

diff <($(echo $latest)) <($(echo $old)) > new_file.txt

Also we have tried below command too.
diff <(echo $latest) <(echo $old) > new_file.txt

Thanks for your concern.[/QUOTE]

have you tried your “diff” invocation from the command line? AFAICT, the script is plain wrong for at least two reasons:

  • you’re re-directing input twice ("<($(echo $latest)) <($(echo $old))") - which file do you expect to replace std in?
  • diff (in “file” mode) works by comparing two two files given as arguments - you don’t give any argument to diff

“diff $old $lastest > new_file.txt” should do the work.

Regards,
Jens

whatever you do, don’t ask this type of question at linuxquestions.org

try this for starters:

#!/bin/bash

diff $1 $2 > diff_output.txt

# -s checks for empty file, if empty file from diff then files are equal

if [ -s diffoutput.txt ]
then
   printf "files differed, emailing diff_output.txt\
"
#  figure out mail command to email diff_output.txt
else
   printf "files were identical, diff_output.txt is an empty file\
"
   printf "deleting diff_output.txt\
"
   rm -f diff_output.txt
fi

the above will do the diff on the 2 files that you provide to the script;

you could give a 3rd argument to the script which you would reference as $3 to be the file name you want to be for diff_output.txt;

a fourth argument which you would reference as $4 in the script could be the email address,
sorry i don’t know off hand how to mail a text file to some address from the command line but a web search should hopefully solve that.

whatever you do, don’t ask this type of question at linuxquestions.org

try this for starters:

#!/bin/bash

diff $1 $2 > diff_output.txt

# -s checks for empty file, if empty file from diff then files are equal

if [ -s diffoutput.txt ]
then
   printf "files differed, emailing diff_output.txt\
"
# figure out mail command to email diff_output.txt
else
   printf "files were identical, diff_output.txt is an empty file\
"
   printf "deleting diff_output.txt\
"
   rm -f diff_output.txt
fi

the above will do the diff on the 2 files that you provide to the script;

you could give a 3rd argument to the script which you would reference as $3 to be the file name you want to be for diff_output.txt;

a fourth argument which you would reference as $4 in the script could be the email address,
sorry i don’t know off hand how to mail a text file to some address from the command line but a web search should hopefully solve that.

to further automate to do this on the 2 files in a directory having the latest date modified,
my thought would be pass the directory into the script above as an argument, then do an “ls -t” command on that argument.
an “ls -t” will dump all file names but the first two would be the ones you want, at this point it’s a matter of extracting those first two file names from the ls -t command.

for automating all this, make it a cron job. I only know and have ever used “crontab -e” while being root to automate system stuff like rotating the audit log. but in cron it’s:

# Minute   Hour   Day of Month       Month          Day of Week        Command    
# (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)

# you would want to do this to have above script run every monday at 2pm:

0  14  *  *   1       /data/bin/thatscriptabove.txt

Sorry, but you’re out of line.

Please, go to LQ and search for ron7000 to see his post. You got an answer (and ignored it), and then asked people to just write a script for you. Why should you have folks write things for you? You were plain lazy at LQ, period.

this makes me laugh, even now you’re telling people to go search.

did you know you can do this: http://www.linuxquestions.org/questions/programming-9/script-help-check-for-empty-file-4175557893/

there’s my post, and again… thanks for not helping least you could’ve done was register here and post something helpful in this thread.

folks,

please stick to technical discussions and handle your private quarrels offline or somewhere else.

Jens