| | |
comparing two directory tree structures
Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2005
Posts: 1
Reputation:
Solved Threads: 0
Hi,
I am Rajesh. I am totally new to shell scripting. I have to prepare a shell script which comapres two directory tree structures ( directory contents comparision is not necessary).
for example, suppose I had installed a software system long back and now I want to upgrade that software but I dont want to disturb the tree stucture. So I had to check the directory tree structure of Updated software with the directory tree strucure of previous version software.
here is the code I am preparing for this problem..but I am not able to achieve my goal. So please, If anyone can review the code and point out where I am going wrong, I would be thankful to them.
the code is :
---------------------------------------------------------------------
#!/bin/bash
#
# compare: compare directory trees recursively and report the differences.
# Author: Rajesh thota
function gettype () {
if [ -d $1 ]; then
echo "directory"
else
echo "notdirectory"
fi
}
function exists () {
if [ -e $1 -o -L $1 ]; then
return 0;
else
echo "rajesh $1 does not exist."
return 1;
fi
}
function comparedirectory () {
local result=0
local v1=0
local v2=0
for i in `(ls -A $1 && ls -A $2) | sort | uniq`; do
# v1=$(gettype $1/$i)
# v2=$(gettype $2/$i)
# echo "$v1"
# echo "$v2"
# if [ ($v1 = "directory") && ($v2 = "directory") ]; then
compare $1/$i $2/$i || result=1
# fi
done
return $result
}
# compare directories
function compare () {
(exists $1 && exists $2) || return 1;
local type1=$(gettype $1)
local type2=$(gettype $2)
echo "$type1"
echo "$type2"
if [ $type1 = $type2 ]; then
comparedirectory $1 $2
else
echo "type mismatch: $type1 ($1) and $type2 ($2)."
false
fi
return
}
if [ 2 -ne $# ]; then
cat << EOU
Usage: $0 dir1 dir2
Compare directory trees:
directories are checked for identical tree stuctures
exit 10
fi
---------------------------------------------------------------------
Thanking you a lot
Regards
Rajesh
I am Rajesh. I am totally new to shell scripting. I have to prepare a shell script which comapres two directory tree structures ( directory contents comparision is not necessary).
for example, suppose I had installed a software system long back and now I want to upgrade that software but I dont want to disturb the tree stucture. So I had to check the directory tree structure of Updated software with the directory tree strucure of previous version software.
here is the code I am preparing for this problem..but I am not able to achieve my goal. So please, If anyone can review the code and point out where I am going wrong, I would be thankful to them.
the code is :
---------------------------------------------------------------------
#!/bin/bash
#
# compare: compare directory trees recursively and report the differences.
# Author: Rajesh thota
function gettype () {
if [ -d $1 ]; then
echo "directory"
else
echo "notdirectory"
fi
}
function exists () {
if [ -e $1 -o -L $1 ]; then
return 0;
else
echo "rajesh $1 does not exist."
return 1;
fi
}
function comparedirectory () {
local result=0
local v1=0
local v2=0
for i in `(ls -A $1 && ls -A $2) | sort | uniq`; do
# v1=$(gettype $1/$i)
# v2=$(gettype $2/$i)
# echo "$v1"
# echo "$v2"
# if [ ($v1 = "directory") && ($v2 = "directory") ]; then
compare $1/$i $2/$i || result=1
# fi
done
return $result
}
# compare directories
function compare () {
(exists $1 && exists $2) || return 1;
local type1=$(gettype $1)
local type2=$(gettype $2)
echo "$type1"
echo "$type2"
if [ $type1 = $type2 ]; then
comparedirectory $1 $2
else
echo "type mismatch: $type1 ($1) and $type2 ($2)."
false
fi
return
}
if [ 2 -ne $# ]; then
cat << EOU
Usage: $0 dir1 dir2
Compare directory trees:
directories are checked for identical tree stuctures
exit 10
fi
---------------------------------------------------------------------
Thanking you a lot
Regards
Rajesh
•
•
Join Date: May 2004
Posts: 178
Reputation:
Solved Threads: 10
Assuming I understand your problem:
Shell Scripting Syntax (Toggle Plain Text)
find /path1 -exec basename {} \; | sort > file1 find /path2 -exec basename {} \; | sort > file2 # at this point you can use diff file1 file2 # or # here we want files only listed in file1 but not in file2 comm -23 file1 file2 # here we show files listed in file2 but not in file1 comm -23 file2 file1
![]() |
Similar Threads
- [Tweak] Various Tips n' Tweaks To Enhance your WinXPerience. (Windows tips 'n' tweaks)
- another newbie with alot of redhat and apache server Q'S (Linux Servers and Apache)
- Directory Trees (C++)
- Help with strings and file directory tree (C)
- Trees? (C)
- DOS issue (Windows NT / 2000 / XP)
- Mixed up Users (Windows NT / 2000 / XP)
Other Threads in the Shell Scripting Forum
- Previous Thread: removing duplicate lines in unix
- Next Thread: Problem with ksh loop
| Thread Tools | Search this Thread |





