Created
March 10, 2015 11:08
-
-
Save sixtyfive/f4f414d0aa7191584dd8 to your computer and use it in GitHub Desktop.
Black Magic Script taken from http://unix.stackexchange.com/questions/49959/how-to-restore-default-group-user-ownership-of-all-files-under-var, adapted for /usr and fixed (there was a typo in on of the sed commands). Be careful with this!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
perm_string_to_mode() { | |
string="$1" | |
let perms=0 | |
[[ "${string}" = ?r???????? ]] && perms=$(( perms + 400 )) | |
[[ "${string}" = ??w??????? ]] && perms=$(( perms + 200 )) | |
[[ "${string}" = ???x?????? ]] && perms=$(( perms + 100 )) | |
[[ "${string}" = ???s?????? ]] && perms=$(( perms + 4100 )) | |
[[ "${string}" = ???S?????? ]] && perms=$(( perms + 4000 )) | |
[[ "${string}" = ????r????? ]] && perms=$(( perms + 40 )) | |
[[ "${string}" = ?????w???? ]] && perms=$(( perms + 20 )) | |
[[ "${string}" = ??????x??? ]] && perms=$(( perms + 10 )) | |
[[ "${string}" = ??????s??? ]] && perms=$(( perms + 2010 )) | |
[[ "${string}" = ??????S??? ]] && perms=$(( perms + 2000 )) | |
[[ "${string}" = ???????r?? ]] && perms=$(( perms + 4 )) | |
[[ "${string}" = ????????w? ]] && perms=$(( perms + 2 )) | |
[[ "${string}" = ?????????x ]] && perms=$(( perms + 1 )) | |
[[ "${string}" = ?????????t ]] && perms=$(( perms + 1001 )) | |
[[ "${string}" = ?????????T ]] && perms=$(( perms + 1000 )) | |
echo $perms | |
} | |
# generate a list of installed packages that have files etc in /usr | |
grep -l /usr/ /var/lib/dpkg/info/*.list | \ | |
sed -e 's:/var/lib/dpkg/info/::' -e 's/\.list$//' | \ | |
xargs dpkg -l | \ | |
awk '/^[hi]/ {print $2}' > /tmp/packages.list | |
# clean out the apt cache, so we only have one version of each package | |
# apt-get clean | |
# download the packages as if we were going to reinstall them | |
# NOTE: packages which are no longer available for download | |
# will not have their permissions fixed. apt-get will complain about | |
# those packages, so you can get a list by redirecting or tee-ing the | |
# output of this script. | |
xargs apt-get -y -d -u --reinstall install < /tmp/packages.list | |
for pkg in $(cat /tmp/packages.list) ; do | |
PKGFILE="/var/cache/apt/archives/${pkg}_*.deb" | |
if [ -e $PKGFILE ] ; then | |
dpkg-deb -c /var/cache/apt/archives/${pkg}_*.deb | \ | |
awk '/\.\/usr\// {print $1, $2, $6}' | \ | |
sed -e 's/ /\t/' -e 's/ /\t/' | \ | |
while IFS=$'\t' read permstring ownergroup filename ; do | |
# don't change owner/group/perms on symlinks | |
if ! [[ "${permstring}" =~ ^l ]] ; then | |
mode=$(perm_string_to_mode $permstring) | |
# change "owner/group" to "owner:group" for chown | |
ownergroup=${ownergroup//\//:} | |
# remove leading '.' from filename | |
filename=${filename#?} | |
chown "$ownergroup" "$filename" | |
chmod "$mode" "$filename" | |
fi | |
done | |
echo | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment