Wednesday, August 4, 2010

An useful shell script to use diff in a directory

Hi I just made this script to use diff in two directories, it fetch the files in the directories and diff them, if it finds a subdirectory it goes in and diff the files inside, I hope you use it and make it better.

You have to name the file recursive_diff.sh and place it in your home, sorry this is my first script and I dont know how to use paths, there are 3 parameters.

To call the script there are 3 parameters:

sh recursive_diff.sh original_directory copy_directory diff_files_firecory

example:

sh recursive_diff.sh my_project my_copy different_files


#!/bin/bash



for i in $(ls $1)
do
    if [ -f "$1/$i" ];then
 filename="$3/$i.diff"
 diff $1/$i $2/$i > $filename
 filesize=$(wc -c < $filename)
        if [ $filesize = 0 ];then
            rm $filename
        fi 
    else
 mkdir $3/$i
 sh $HOME/recursive_diff.sh $1/$i $2/$i $3/$i
    fi
done