The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
mget * (obtein files from current directory but not the files form sub-directories) Peter321 Shell Programming and Scripting 0 03-12-2009 11:59 AM
compare db zone files in 2 directories richsark Shell Programming and Scripting 2 03-05-2009 11:05 AM
how many directories and files are there in a directory smongam Shell Programming and Scripting 4 01-22-2009 02:47 PM
Compare files from two directories ravi214u Shell Programming and Scripting 2 07-14-2008 08:04 AM
How to compare two flat files and get changed data jtshashidhar Shell Programming and Scripting 3 01-29-2006 10:26 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 2 votes, 4.50 average. Display Modes
  #1 (permalink)  
Old 05-26-2009
bkeep bkeep is offline
Registered User
  
 

Join Date: May 2009
Location: Freeport, IL
Posts: 9
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 etc and dist_old/file1.php dist_old/file2.php etc.

The third folder contains only the changed files between the two. If /dist/file1.php differs from /dist_old/file1.php then a file named dist_upgrade/file1.php exists with the same contents as dist/file1.php.

Currently I am manually coping the different files using winmerge and I would like to automate some of this if possible.

What I would like to do is compare the dist folder with the dist_old folder then have files that are not the same copied to dist_upgrade/file.ext retaining the directory structure and full contents of the new file in the dist folder.

Thanks for any advice,
Best Regards,
Brandon
  #2 (permalink)  
Old 05-26-2009
edgarvm edgarvm is offline
Registered User
  
 

Join Date: May 2009
Posts: 26
This could be useful

Code:
#!/bin/sh
cd dist
for a in `ls`; do
   
   if [ ! -f "../dist_old/$a" ]; then
       continue
   fi
   diff $a ../dist_old/$a > /dev/null
   if [[ "$?" == "1" ]]; then
      cp $a ../dist_upgrade
   fi
done


Last edited by edgarvm; 05-26-2009 at 07:35 PM..
  #3 (permalink)  
Old 05-26-2009
JerryHone JerryHone is offline
Registered User
  
 

Join Date: Nov 2006
Location: UK
Posts: 178
Similar to edgarvm's but coping with new files as well as different ones...


Code:
new=/dist
old=/dist_old
diff=/dist_upgrade

cd $new
for f in *f
do
# Diff the files - ignore the output...
    diff $f $old > /dev/null 2>&1
# ...but get the status
    status=$?
    if [ $status -eq 0 ] ; then
# Files are identical - don't copy the file
        echo $f unchanged
    elif [ $status -eq 1 ] ; then
# Files differ - copy new file
        echo $f changed
        cp $f $diff
    elif [ $status -eq 2 ] ; then
# Old file doesn't exist - copy new file
        echo old $f does not exist
        cp $f $diff
    fi
done

You'll actually get a status 2 if a file exists but is unreadable as well, but I've ignored that as you can make everything readable before you start!
  #4 (permalink)  
Old 05-26-2009
bkeep bkeep is offline
Registered User
  
 

Join Date: May 2009
Location: Freeport, IL
Posts: 9
Thanks for the responses you guys are top notch. I have made a few modifications to get this working recursively using edgarvm's example. I also incorporated JerryHone's idea about dealing with non-existent files


Code:
#!/bin/bash

# setup folders for our different stages
DIST=/var/www/localhost/htdocs/dist/
DIST_OLD=/var/www/localhost/htdocs/dist_old/
DIST_UPGRADE=/var/www/localhost/htdocs/dist_upgrade/

cd $DIST

list=`find . -type f`

for a in $list; do
   if [ ! -f "$DIST_OLD$a" ]; then
        cp --parents $a $DIST_UPGRADE
      continue
   fi
   diff $a $DIST_OLD$a > /dev/null
   if [[ "$?" == "1" ]]; then
        # File exists but is different so copy changed file
        cp --parents $a $DIST_UPGRADE
   fi
done

Thanks for the advice.
Regards,
Brandon

Last edited by bkeep; 05-26-2009 at 10:04 PM.. Reason: Got it working with above code
  #5 (permalink)  
Old 08-26-2009
realaaa realaaa is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 1
thanks you guys, this script helped me a lot!

but today I found out that it can't process spaces in file names properly so I modified it a bit using the advise from here:

Unixjunkie Blog: Handling Filenames With Spaces


this script seems to process such files properly (although I didn't test it too much):


Code:
#!/bin/bash

# setup folders for our different stages
DIST=/var/www/localhost/htdocs/dist/
DIST_OLD=/var/www/localhost/htdocs/dist_old/
DIST_UPGRADE=/var/www/localhost/htdocs/dist_upgrade/

cd $DIST

find . -type f | while read filename
do
   if [ ! -f "$DIST_OLD$filename" ]; then
        cp --parents "$filename" $DIST_UPGRADE
      continue
   fi
   diff "$filename" "$DIST_OLD$filename" > /dev/null
   if [[ "$?" == "1" ]]; then
        # File exists but is different so copy changed file
        cp --parents $filename $DIST_UPGRADE
   fi
done

cheers!
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:46 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0