#!/bin/bash function countFiles { IFS=" " #statements nf=$(ls -l "$1" | grep "^-.*" | wc -l) # count all files nd=$(ls -l "$1" | grep "^d.*" | wc -l) # count lines of all dirs in thing fles=$(expr "$fles" + "$nf") dirs=$(expr "$dirs" + "$nd") for i in $(ls -l "$1" | grep "^d.*" | grep -o "[^ ]\+$") # for every directory in current dir do # echo "2: $2" countFiles "$2/$i" "$2/$i" # count that directory done } function countHidden { IFS=" " #statements nhf=$(ls -la "$1" | grep "^-.*" | wc -l) # count all files nhd=$(ls -la "$1" | grep "^d.*[^\.]$" | wc -l) # count lines of all dirs in thing EXCEPT . and .. truefiles=$(expr "$truefiles" + "$nhf") truedirs=$(expr "$truedirs" + "$nhd") for i in $(ls -la "$1" | grep "^d.*[^\.]$" | grep -o "[^ ]\+$") # for every directory in current dir do # echo "2: $2" countHidden "$2/$i" "$2/$i" # count that directory done } function doTheThing { filenames=$(ls -R $1) # -R the directory tree (recursively) echo "Permissions for:" currPath=$1 for f in $filenames do pathExists=$( echo $f | grep ":$") # if the current $f ends in a colon if [ $pathExists ] then currPath=$( echo $f | cut -d: -f1) # using delimiter : cut remove the first field - i.e. remove : else filePath=$currPath/$f # means that current selected is not a path. permissions=$(ls -dl $filePath | cut -c 1-10) # get the current -l of selected file, cut out chars 1-10 # this gets the -rwxrwxrwx thing echo "$f: $permissions" # check if it is a directory if [ $(ls -dl $filePath | grep "^d" | cut -c 1) ] # if file does not have d, then it returns nothing then if [ $permissions != "drwx--x--x" ] # if permissions do not match up for file then echo "Directory $filePath has wrong permissions: $permissions" $(chmod 711 $filePath) # change permissions ofpath echo "Changed to drwx--x--x" fi else # must be a file and not a dir filetype=$(echo "$filePath" | cut -d. -f2) # cut everything after . to get extension if [ $filetype == "html" ] || [ $filetype == "css" ] || [ $filetype == "txt" ] # htmlcss file should be 744 then if [ $permissions != "-rwxr--r--" ] then echo "File $filePath has wrong permissions: $permissions" $(chmod 744 $filePath) echo "Changed to -rwxr--r--" fi elif [ $filetype ] # all oher files should be 700 then if [ $permissions != "-rwx------" ] then echo "File $filePath has wrong permissions: $permissions" $(chmod 700 $filePath) echo "Changed to -rwx------" fi fi fi fi done } function main { #statements fles=0 dirs=0 truefiles=0 truedirs=0 countFiles $1 $1 countHidden $1 $1 diffhidden=$(expr "$truefiles" - "$fles") diffhiddir=$(expr "$truedirs" - "$dirs") echo "Files found: $fles (plus $diffhidden hidden)" echo "Directories found: $dirs (plus $diffhiddir hidden)" echo "Total files and directories: $(expr "$truefiles" + "$truedirs")" doTheThing $1 } main $1