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

**URL:** <https://forums.suse.com/t/script-to-compare-latest-two-files-using-diff-command/27431>\
**Category:** SLES Configure-Administer\
**Created:** [September 15, 2015, 12:24pm UTC](https://forums.suse.com/t/script-to-compare-latest-two-files-using-diff-command/27431 "2015-09-15T12:24:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Santosh\_2411](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@Santosh\_2411](https://forums.suse.com/u/Santosh_2411)\
**Post date:** [September 15, 2015, 12:24pm UTC](https://forums.suse.com/t/script-to-compare-latest-two-files-using-diff-command/27431/1 "2015-09-15T12:24:26Z")

</div>

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… 🙂

-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

---

<div class="post-metadata">

**Author:** ![Jens-U](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@Jens-U](https://forums.suse.com/u/Jens-U)\
**Post date:** [September 15, 2015, 1:02pm UTC](https://forums.suse.com/t/script-to-compare-latest-two-files-using-diff-command/27431/2 "2015-09-15T13:02:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ab1](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@ab1](https://forums.suse.com/u/ab1)\
**Post date:** [September 15, 2015, 5:02pm UTC](https://forums.suse.com/t/script-to-compare-latest-two-files-using-diff-command/27431/3 "2015-09-15T17:02:13Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Santosh\_2411](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@Santosh\_2411](https://forums.suse.com/u/Santosh_2411)\
**Post date:** [September 16, 2015, 7:41am UTC](https://forums.suse.com/t/script-to-compare-latest-two-files-using-diff-command/27431/4 "2015-09-16T07:41:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jens-U](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@Jens-U](https://forums.suse.com/u/Jens-U)\
**Post date:** [September 16, 2015, 10:25am UTC](https://forums.suse.com/t/script-to-compare-latest-two-files-using-diff-command/27431/5 "2015-09-16T10:25:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ron7000](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@ron7000](https://forums.suse.com/u/ron7000)\
**Post date:** [November 10, 2015, 6:50am UTC](https://forums.suse.com/t/script-to-compare-latest-two-files-using-diff-command/27431/6 "2015-11-10T06:50:52Z")

</div>

whatever you do, don’t ask this type of question at [linuxquestions.org](http://linuxquestions.org)

try this for starters:

```auto
#!/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.

---

<div class="post-metadata">

**Author:** ![ron7000](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@ron7000](https://forums.suse.com/u/ron7000)\
**Post date:** [November 10, 2015, 6:59am UTC](https://forums.suse.com/t/script-to-compare-latest-two-files-using-diff-command/27431/7 "2015-11-10T06:59:16Z")

</div>

whatever you do, don’t ask this type of question at [linuxquestions.org](http://linuxquestions.org)

try this for starters:

```auto
#!/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:

```auto
# 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
```

---

<div class="post-metadata">

**Author:** ![pwilson](https://avatars.discourse-cdn.com/v4/letter/p/bb73d2/32.png) [@pwilson](https://forums.suse.com/u/pwilson)\
**Post date:** [November 11, 2015, 12:15am UTC](https://forums.suse.com/t/script-to-compare-latest-two-files-using-diff-command/27431/8 "2015-11-11T00:15:43Z")

</div>

> [@ron7000;30336](#):
>
> whatever you do, don’t ask this type of question at [linuxquestions.org](http://linuxquestions.org)

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.

---

<div class="post-metadata">

**Author:** ![ron7000](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@ron7000](https://forums.suse.com/u/ron7000)\
**Post date:** [November 17, 2015, 8:58pm UTC](https://forums.suse.com/t/script-to-compare-latest-two-files-using-diff-command/27431/9 "2015-11-17T20:58:52Z")

</div>

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/](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.

---

<div class="post-metadata">

**Author:** ![Jens-U](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@Jens-U](https://forums.suse.com/u/Jens-U)\
**Post date:** [November 17, 2015, 9:54pm UTC](https://forums.suse.com/t/script-to-compare-latest-two-files-using-diff-command/27431/10 "2015-11-17T21:54:05Z")

</div>

folks,

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

Jens
