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