Created
September 21, 2012 15:53
-
-
Save josephspurrier/3762289 to your computer and use it in GitHub Desktop.
This script creates a text file that contains all the current permissions of the entire linux system.
This file contains 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 | |
### ——————————————————————– ### | |
# This script creates a text file that contains all the current permissions | |
# of the entire linux system. | |
# Run: sudo bash get_all_permissions.sh | |
# Location: http://virtualparadise.wordpress.com/2010/04/29/linux-repair-permissions | |
# Date Created: 2/16/2010 | |
# Date Modified: 2/16/2010 | |
# Permissions Required: Run as root | |
# Notes: Errors will appear in the /proc directory, these are expected | |
### ——————————————————————– ### | |
# Converts the r,w, and x characters into number values | |
# for use with chmod | |
function getValue { | |
VALUE=0 | |
R=$(echo $1 | awk '{print substr($1, 1, 1)}') | |
W=$(echo $1 | awk '{print substr($1, 2, 1)}') | |
X=$(echo $1 | awk '{print substr($1, 3, 1)}') | |
if [ "$R" = "r" ]; then | |
VALUE=`expr $VALUE + 4` | |
fi | |
if [ "$W" = "w" ]; then | |
VALUE=`expr $VALUE + 2` | |
fi | |
if [ "$X" = "x" ]; then | |
VALUE=`expr $VALUE + 1` | |
fi | |
echo $VALUE | |
} | |
# Separates each of the different categories of permissions | |
# and then passed them to the conversion function. | |
# Finally, creates a string with all 3 numbers side by side | |
function getChmod { | |
USERSECT=$(echo $1 | awk '{print substr($1, 2, 3)}') | |
GROUPSECT=$(echo $1 | awk '{print substr($1, 5, 3)}') | |
WORLDSECT=$(echo $1 | awk '{print substr($1, 8, 3)}') | |
USERV=$(getValue $USERSECT) | |
GROUPV=$(getValue $GROUPSECT) | |
WORLDV=$(getValue $WORLDSECT) | |
echo $USERV$GROUPV$WORLDV #:$2 | |
} | |
function isValid { | |
case $1 in | |
-) | |
return 1;; | |
d) | |
return 1;; | |
l) | |
return 1;; | |
*) | |
return 0;; | |
esac | |
} | |
# Set the debug value to display additional output | |
DEBUG=false | |
# List the entire filesystem | |
ls -Rl / > permissions.txt | |
# Value prefixed to each file name of the current | |
# directory | |
CURDIR="" | |
# Loop through the result of ls-RL on the root | |
# and parse through extracting the permissions, | |
# owners, and full file names. | |
while read LINE | |
do | |
# First column – Permissions | |
COL1=$(echo $LINE | awk -F" " '{print $1}') | |
# Third column – User | |
COL3=$(echo $LINE | awk -F" " '{print $3}') | |
# Fourth column – Group | |
COL4=$(echo $LINE | awk -F" " '{print $4}') | |
# Ninth column – File name | |
COL9=$(echo $LINE | awk -F" " '{print $9}') | |
# First charater | |
CHAR1=$(echo $LINE | awk '{print substr($1, 1, 1)}') | |
# If the line is a directory | |
if [ "$CHAR1" = "/" ]; then | |
# Remove the last colon | |
CURDIR=$(echo $LINE | sed 's/:/\//') | |
# Replace // with / if found | |
CURDIR=$(echo $CURDIR | sed 's/\/\//\//') | |
fi | |
# If the line is a file, link, or directory | |
isValid $CHAR1 | |
if [ "$?" = "1" ]; then | |
# Get the chmod numeric permissions value | |
CHMODVALUE=$(getChmod $COL1 $LINE) | |
# If debugging, output additional variables | |
if [ "$DEBUG" = true ]; then | |
echo "" | |
echo $LINE | |
echo $CURDIR$COL9 | |
echo $CHMODVALUE | |
echo $COL3 | |
echo $COL4 | |
fi | |
# Output the chmod command | |
echo "chmod $CHMODVALUE -R $CURDIR$COL9" | |
# Output the chown command | |
echo "chown $COL3:$COL4 -R $CURDIR$COL9" | |
fi | |
done < permissions.txt | |
exit | |
#for file in `cat permissions.txt`; do | |
# echo $file | |
#done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment