Recently I needed to compare some permissions between two directories, I wrote this script that could be useful for someone else:
12345678910111213141516171819202122
#!/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=""
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"]; thenorig=$1elif[ -z "$targ"]; thentarg=$1elseecho"Unknown flag $1" showusage
exit 1
fi esacshiftdoneif[ -z "$orig" -o -z "$targ"]; thenshowusage
exit 1
fi# Change into the directory and get a list of files and dirspushd . >/dev/null
cd$origlist=`find .`popd >/dev/null
# Go through the listfor i in $list; do# Get the permissions of the orig file and targ filep=`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" !=""]; thenecho"$targ/$i not present"fi else# Permission for targ file, compareif["$p" !="$p1"]; then# Permissions are different going to try to set permissionsif["$apply_changes" !=""]; then# Set permissionse=`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 fidone
Comments