Unix/Linux Forum: Need help with shell script to compare files in two dirs - Unix/Linux Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Need help with shell script to compare files in two dirs Rate Topic: -----

#1 User is offline   fierusbentus Icon

  • Newbie (User Level: 0/10)
  • Group: Members
  • Posts: 1
  • Joined: 03-March 09

Posted 03 March 2009 - 12:10 PM

I have a directory which is /home/mark/files/ , inside this particular I have a bunch of filles (see examples below)

TST_SHU_00014460_20090302.txt
TST_SHU_00016047_20090302.txt
TST_SHU_00007838_20090303.txt
TST_SHU_00056485_20090303.txt
TST_SHU_00014460_20090303.txt
TST_SHU_00014176_20090303.txt

And I also have another directory which is /home/dest/ which has the following dirs

00014460
00016047
00056485

As you can see, the directories matches a part of the files, inside each one of these directories i have subdirectories and anywhere inside these dirs the same files as in the first dir is copied there

I would like to have a shell script that would get the latest 10 file names created on the first dir (/home/mark/files/) and find them on the second dir /home/dest/ and when a file on the first directory has 0 bytes but on the second directory has data send me an e-mail and viscebersa when a file on the second directory has 0 bytes but the files on the first directory has data send me an email.

Thanks
0

#2 User is offline   eggi Icon

  • User Level: 6/10
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 311
  • Joined: 25-November 07
  • Location:Grayslake, IL USA

Posted 08 March 2009 - 08:09 AM

Hey there,

Here's a little command line version of what you need:

host # ls -tr files|tail -10|while read x;do if [ `du -sk files/$x|cut -f1` -eq 0 -o $(du -sk `find dest -name $x`|cut -f1) -eq 0 ]; then ls -l files/$x; ls -l `find dest -name $x`;fi;done

Quote

-rw-r--r-- 1 mgolvach None 0 Mar 8 01:50 files/TST_SHU_00056485_20090303.txt
-rw-r--r-- 1 mgolvach None 5 Mar 8 01:57 dest/00056485/TST_SHU_00056485_20090303.txt
-rw-r--r-- 1 mgolvach None 3 Mar 8 01:50 files/TST_SHU_00014460_20090303.txt
-rw-r--r-- 1 mgolvach None 0 Mar 8 01:56 dest/00014460/TST_SHU_00014460_20090303.txt
host #


Just replace the:
	ls -l files/$x; ls -l `find dest -name $x`

with your sendmail or mailx, etc, command.

Hope that helps :)

, Mike
The greatest viral marketing idea of all time, get your copy of this Free Report now!
----
Linux Tips, Trick and Advice -- The Linux and Unix Menagerie
0

#3 User is offline   eggi Icon

  • User Level: 6/10
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 311
  • Joined: 25-November 07
  • Location:Grayslake, IL USA

Posted 08 March 2009 - 08:17 AM

Just a BTW,

If it helps, here's that command line in script form:

#!/bin/bash

ls -tr files|tail -10|while read x
do
  if [ `du -sk files/$x|cut -f1` -eq 0 -o $(du -sk `find dest -name $x`|cut -f1) -eq 0 ]
  then
	  ls -l files/$x; ls -l `find dest -name $x`
  fi
done


and if you have problems with variables inside the subshell created by piping to the while loop, you can write it with a simple redirect like this:

#!/bin/bash

while read x
do
  if [ `du -sk files/$x|cut -f1` -eq 0 -o $(du -sk `find dest -name $x`|cut -f1) -eq 0 ]
  then
	  ls -l files/$x; ls -l `find dest -name $x`
  fi
done <<<"`ls -tr files|tail -10`"


Best wishes,

Mike
The greatest viral marketing idea of all time, get your copy of this Free Report now!
----
Linux Tips, Trick and Advice -- The Linux and Unix Menagerie
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Cambridge Plus :: Cambridgeshire Steam Rally :: PIC Micro Development :: Brico Piston Rings
Unix Man Pages / Linux Man Pages :: HiFi Forum :: A14 Comments :: UNIX/Linux Forum Archives