Change all the file and directory “group” permissions to match the owner’s permissions

June 23rd, 2010 | rpickett | Technical

A buddy of mine accidentaly zapped all the group permissions on his files and needed a recursive script that would make all the group perms match the owner perms. It was a neat little exercise (took about 20 min to write and “confirm”.

Running as is will just output the current file permissions and what the chown will look like. When you’re ready to let it fly, just uncomment the next to the last line.

#!/bin/bash

find . | grep -v '^\.$' | while read line; do
	full_perms=`ls -l "$line" | awk '{print $1}'`
	perms=${full_perms:1:3}
	add=""
	del=""
	for p in r w x; do
		if [ -n "`echo $perms | grep $p`" ]; then
			add="${add}$p"
		else
			del="${del}$p"
		fi
	done

	perms=""
	if [ -n "$add" ]; then
		if [ -n "$del" ]; then
			perms="g+${add},g-${del}"
		else
			perms="g+${add}"
		fi
	else
		if [ -n "$del" ]; then
			perms="g-${del}"
		fi
	fi

	echo $full_perms :: chmod "$perms" "$line"
	# chmod "$perms" "$line"
done

Leave a Comment