Compare files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare files
# 1  
Old 08-31-2005
Compare files

Sup,
I am trying to write a script that will check to see if any files have been deleted or created in a directory within a space of time (say 10 - 20 mins). I haven't used the comm or cmp commands before and I think that is what I could use and I am getting lost, anybody go ideas
# 2  
Old 08-31-2005
You could use the following logic.

1. Print the contents of the directory as a list and save it.
2. Print the contents of the directory as a list, after 10 mins and save it.

And then use these lists with any solution provided in this post

Vino
# 3  
Old 08-31-2005
no too sure what you mean with the $(1) and $(2) variables, sorry to be a n00b but could you explain it a bit more? How do I use the two lists of files?
# 4  
Old 08-31-2005
They are known as positional parameters.

From the man pages of bash

Code:
 Any arguments
              remaining after the options are processed are treated as  values
              for the positional parameters and are assigned, in order, to $1,
              $2, ...  $n.

That means any command line parameters passed to the script will be stored in $1, $2 ... and so on.

If n > 9 , then the positional parameters are accessed as ${10}, ${11}.. and so on.

Here ${1} stands for the first list and ${2} and for the second list, i.e. the one generated after 10 mins.

${0) stands for the name of the calling script.

Vino
# 5  
Old 08-31-2005
Why not use the Ruby language? It's free and available for almost all platforms.

Save the code as "dir-mon.rb" and run it with
ruby dir-mon.rb mydirectory

Code:
secs_to_sleep = 600

if ARGV.size == 0
  puts "I need the name of the directory."
  exit
end

dir = File.join( ARGV[0], "*" )
old = Dir[ dir ]

loop {
  sleep( secs_to_sleep )
  new = Dir[ dir ]
  
  print "Files deleted: " ; p old - new
  print "Files added: " ; p new - old
  puts

  old = new
}


Last edited by futurelet; 09-05-2005 at 06:20 AM.. Reason: small improvement
# 6  
Old 09-04-2005
How would I go about making this an automatic script that I could leave. So I just run the script and it automatically sleeps for 10 mins then compares that 10 min file to the last one, basically so I can come back after an hour or so and I would see a list like:

No txt files have been created or removed in the last 10 mins.

No txt files have been created or removed in the last 10 mins.

The following txt file(s) have been created in the last 10 mins:

a3.txt
a4.txt
a5.txt
The following txt file(s) have been removed in the last 10 mins:

a4.txt
a5.txt
No txt files have been created or removed in the last 10 mins.

No txt files have been created or removed in the last 10 mins.

... ... ... ... ...
# 7  
Old 09-05-2005
The script should be pretty straightforward:

Supposing the directory has not overly many entries (arrays can only be 1024 elements in ksh):

Code:
1. read filenames and -sizes of all files in the directory to two arrays

in a while-loop do:
   1. sleep 10 mins
   2. read the contents of the content of the directory to two new arrays
      (filename and size), create a third array of the same size initalized with "0"s
   3. loop through the first array and throw out any entry you can find in the
      new array and which filesize hasn't changed, flag the file with a "1" in the 
      third array
   3a. if the filesize has changed print out "file changed" and drop it then after
       flagging it in the third array the same way
   4. print any arrayelement left in the old array as deleted
   5. print any arrayelement in the new array with a "0" in the third array as 
      "new file"
   6. drop the two old arrays and the third array with the flags and make the
      two new ones the old one.
done /* while-loop */

The coding of the script is left as an exercise to the reader. ;-))

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare files and share output from both files

hi all, Thanks to all for your great help... I have a scenario that I have two files (file1 & file2). I need to compare two files entire row by row and share the output if any discrepancies within two files. File1: DB1|TB1|C1,C3 DB2|TB2|C1,C2 DB3|TB3|C1,C2,C3,C4 File2: ... (2 Replies)
Discussion started by: Selva_2507
2 Replies

2. Shell Programming and Scripting

Compare multiple files, and extract items that are common to ALL files only

I have this code awk 'NR==FNR{a=$1;next} a' file1 file2 which does what I need it to do, but for only two files. I want to make it so that I can have multiple files (for example 30) and the code will return only the items that are in every single one of those files and ignore the ones... (7 Replies)
Discussion started by: castrojc
7 Replies

3. Shell Programming and Scripting

Compare two files, then overwrite first file with only that in both files

I want to compare two files, and search for items that are in both. Then override the first file with that containing only elements which were in both files. I imagine something with diff, but not sure. File 1 One Two Three Four Five File 2 One Three Four Six Eight (2 Replies)
Discussion started by: castrojc
2 Replies

4. Shell Programming and Scripting

Compare files

Please help me with awk.I have two files with the below details file1 123456789 2012 987654321 2011 a1234567892012 a1234abcde2012 b1234567892012 c1234567892012 98765a12342012 file2 a1234 01234 b1234 33333 I need to check whether the items in file2 is present in file1 .If it is... (2 Replies)
Discussion started by: Mary James
2 Replies

5. Shell Programming and Scripting

Require compare command to compare 4 files

I have four files, I need to compare these files together. As such i know "sdiff and comm" commands but these commands compare 2 files together. If I use sdiff command then i have to compare each file with other which will increase the codes. Please suggest if you know some commands whcih can... (6 Replies)
Discussion started by: nehashine
6 Replies

6. Shell Programming and Scripting

Compare 2 folders to find several missing files among huge amounts of files.

Hi, all: I've got two folders, say, "folder1" and "folder2". Under each, there are thousands of files. It's quite obvious that there are some files missing in each. I just would like to find them. I believe this can be done by "diff" command. However, if I change the above question a... (1 Reply)
Discussion started by: jiapei100
1 Replies

7. Shell Programming and Scripting

How to compare 2 files & get only few columns based on a condition related to both files?

Hiiiii friends I have 2 files which contains huge data & few lines of it are as shown below File1: b.dat(which has 21 columns) SSR 1976 8 12 13 10 44.00 39.0700 70.7800 7.0 0 0.00 0 2.78 0.00 0.00 0 0.00 2.78 0 NULL ISC 1976 8 12 22 32 37.39 36.2942 70.7338... (6 Replies)
Discussion started by: reva
6 Replies

8. Shell Programming and Scripting

compare files in two directories and output changed files to third directory

I have searched about 30 threads, a load of Google pages and cannot find what I am looking for. I have some of the parts but not the whole. I cannot seem to get the puzzle fit together. I have three folders, two of which contain different versions of multiple files, dist/file1.php dist/file2.php... (4 Replies)
Discussion started by: bkeep
4 Replies

9. Shell Programming and Scripting

compare two files and to remove the matching lines on both the files

I have two files and need to compare the two files and to remove the matching lines from both the files (4 Replies)
Discussion started by: shellscripter
4 Replies

10. Shell Programming and Scripting

compare two files

I have file1 and file2: file1: 11 xxx kksd ... 22 kkk kdsglg... 33 sss kdfjdksa... 44 kdsf dskjfkas ... hh kdkf kdkkd.. jg dkf dfkdk ... ... file2: jg 22 hh ... I need to check each line of file1. if the field one is in file2, I will keep it; if not, the whole line will be... (17 Replies)
Discussion started by: fredao
17 Replies
Login or Register to Ask a Question