Compare Permissions Script

Recently I needed to compare some permissions between two directories, I wrote this script that could be useful for someone else:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash
# This script will go through a list of files and directories and match their
# permissions against another directory, it can also copy the permissions from
# the first directory.
#
# Written by Pablo Fernandez
#

function showusage()
{
      echo "USAGE: $0 OPTS DIR1 DIR2"
      echo
     echo "OPTIONS:"
      echo "-a Apply changes (update permissions of DIR2"
      echo "-w Warning on missing files"
      echo "-q Quiet mode"
      echo "-h Show this help"
      echo "The directory DIR1 will be matched against DIR2. The directory DIR2 will"
      echo "be corrected with all permission differences."
}

apply_changes=""
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
while [ ! -z "$1" ]; do
      case "$1" in
              -a)
                      apply_changes=1
                      ;;
              -w)
                      warn_on_missing=1
                      ;;
              -q)
                      quiet=1
                      ;;
              -h)
                      showusage
                      exit 0
                      ;;
              *)
                      if [ -z "$orig" ]; then
                              orig=$1
                      elif [ -z "$targ" ]; then
                              targ=$1
                      else
                              echo "Unknown flag $1"
                              showusage
                              exit 1
                      fi
      esac
      shift
done

if [ -z "$orig" -o -z "$targ" ]; then
      showusage
      exit 1
fi

# Change into the directory and get a list of files and dirs
pushd . >/dev/null
cd $orig
list=`find .`
popd >/dev/null

# Go through the list
for i in $list; do
      # Get the permissions of the orig file and targ file
      p=`stat -c %a $orig/$i`
      p1=`stat -c %a $targ/$i 2>/dev/null`

      [ "$quiet" = "" ] && echo -n "$i: "

      if [ "$p1" = "" ]; then
              # No permissions for targ file, targ file doesn't exist
              [ "$quiet" = "" ] && echo "not present"

              if [ "$warn_on_missing" != "" -a "$quiet" != "" ]; then
                      echo "$targ/$i not present"
              fi
      else
              # Permission for targ file, compare
              if [ "$p" != "$p1" ]; then
                      # Permissions are different going to try to set permissions
                      if [ "$apply_changes" != "" ]; then
                              # Set permissions
                              e=`chmod $p $targ/$i 2>&1 >/dev/null`

                              if [ "$?" != "0" ]; then
                                      [ "$quiet" = "" ] && echo "different permission, change to $p failed, $e"
                                      [ "$quiet" != "" ] && echo "$e"
                              else
                                      [ "$quiet" = "" ] && echo "set to $p"
                              fi
                      else
                              # Running just for report
                              [ "$quiet" = "" ] && echo "different, orig is $p targ is $p1"
                              [ "$quiet" != "" ] && echo "Permission of $targ/$i is different than $orig/$i"
                      fi
              else
                      [ "$quiet" = "" ] && echo "Ok"
              fi
      fi
done

Comments