Skip to content
September 3, 2015 / doganay

COMPARE PERMISSIONS OF 2 DIRECTORIES in UNIX/LINUX

#!/usr/bin/ksh

usage()
{
 echo
 echo "#####################################################################"
 echo "##                                                                 ##"
 echo "## diffdirs.sh v1.0                                                ##"
 echo "## Mustafa DOGANAY - 2015                                          ##"
 echo "## https://doganay.wordpress.com                                    ##"
 echo "##                                                                 ##"
 echo "#####################################################################"
 echo "##                                                                 ##"
 echo "## Usage : diffdirs.sh   [OPTIONS]                                 ##"
 echo "## Note  : Please use relative directory names, not absolute path! ##"
 echo "##                                                                 ##"
 echo "#####################################################################"
 echo "##                                                                 ##"
 echo "## Available Options:                                              ##"
 echo "## -p: permissions                                                 ##"
 echo "## -o: owner                                                       ##"
 echo "## -g: group                                                       ##"
 echo "## -s: size                                                        ##"
 echo "## -d: date/time                                                   ##"
 echo "##                                                                 ##"
 echo "#####################################################################"
 echo
}

if [ $# -lt 2 -o $# -gt 7 ];then
 usage
 exit 1
else
 ls -ld $1 2>/dev/null |grep "^d" 1>/dev/null
 if [ $? -ne 0 ];then
  echo
  echo "Error: Parameter 1 is not a directory."
  usage
  exit 2
 fi
 ls -ld $2 2>/dev/null |grep "^d" 1>/dev/null
 if [ $? -ne 0 ];then
  echo
  echo "Error: Parameter 2 is not a directory."
  usage
  exit 3
 fi

 PARAMETERS="X"$3$4$5$6$7

 echo $PARAMETERS | grep "p" 1>/dev/null
 if [ $? -eq 0 ];then
  AWKSTR=$AWKSTR"\$1"
 fi

 echo $PARAMETERS | grep "o" 1>/dev/null
 if [ $? -eq 0 ];then
  AWKSTR=$AWKSTR"\" \"\$3"
 fi

 echo $PARAMETERS | grep "g" 1>/dev/null
 if [ $? -eq 0 ];then
  AWKSTR=$AWKSTR"\" \"\$4"
 fi

 echo $PARAMETERS | grep "s" 1>/dev/null
 if [ $? -eq 0 ];then
  AWKSTR=$AWKSTR"\" \"\$5"
 fi

 echo $PARAMETERS | grep "d" 1>/dev/null
 if [ $? -eq 0 ];then
  AWKSTR=$AWKSTR"\" \"\$6\" \"\$7\" \"\$8"
 fi
fi

find $1 2>/dev/null | xargs ls -ltr 2>/dev/null | awk '{print '"$AWKSTR"'" "substr($NF,index($NF,"/")+1,length($NF)-index($NF,"/"))}' | sort > /tmp/diffdirs.tmp.1
find $2 2>/dev/null | xargs ls -ltr 2>/dev/null | awk '{print '"$AWKSTR"'" "substr($NF,index($NF,"/")+1,length($NF)-index($NF,"/"))}' | sort > /tmp/diffdirs.tmp.2

comm -23 /tmp/diffdirs.tmp.1 /tmp/diffdirs.tmp.2 > /tmp/diffdirs.tmp.3

if [ -s /tmp/diffdirs.tmp.3 ];then
 echo
 echo "Different in Directory1:"
 cat /tmp/diffdirs.tmp.3
else
 echo
 echo "No difference in Directory1."
fi

comm -23 /tmp/diffdirs.tmp.2 /tmp/diffdirs.tmp.1 > /tmp/diffdirs.tmp.3

if [ -s /tmp/diffdirs.tmp.3 ];then
 echo
 echo "Different in Directory2:"
 cat /tmp/diffdirs.tmp.3 
 echo
else
 echo
 echo "No difference in Directory2."
 echo
fi

rm /tmp/diffdirs.tmp.1 /tmp/diffdirs.tmp.2 /tmp/diffdirs.tmp.3 2>/dev/null

exit 0
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: