Student Account clean up script
Execute the following set of steps via a Terminal to clean the Student accounts in Linux machines:
- Login to machine as root.
- If logged in as “beck” then type “sudo su – root” to be root user.
- Download and copy the script embedded below to “/root” of the machine.
- Changed current working directory to “/root”.
- Enable execution rights for this script –> “chmod 755 studentAccountClean.sh”
- Invoke the script by typing “./studentAccountClean.sh”
# This script is used to reset Student accounts in a Linux machine.
# Created by Vimal Menon on 2 January 2017
TIMESTAMP=`date ‘+%m-%d-%Y %H:%M %p’`
# Standard folders in a Student account
export SCRIPTHOME=$(pwd)
export STUDENTHOME=/home/student
# Temporary files created to aid the cleanup activity
FOLDERLIST=FolderDeleteList.txt
FILESLIST=FilesDeleteList.txt
COMPLETIONTIME=StudentAccCleanupTime.txt
# List of Standard folders which should not be deleted. However, its contents should be cleared.
StdFolders=”Desktop Documents Downloads Music Pictures Public Templates Videos”
# Getting the list of Folders and Files at STUDENTHOME
cd $STUDENTHOME
find * -maxdepth 0 -type d > $SCRIPTHOME/$FOLDERLIST
find * -maxdepth 0 -type f > $SCRIPTHOME/$FILESLIST
# If Directories or Filenames have special characters such as (,),[,]{,}, then an escape sequence is introduced to facilitate deletion.
sed -e ‘s/\\/\\\\/g’ -e ‘s/(/\\(/g’ -e ‘s/)/\\)/g’ -e ‘s/{/\\{/g’ -e ‘s/}/\\}/g’ -e ‘s/\[/\\[/g’ -e ‘s/]/\\]/g’ -e ‘s/ /\\ /g’ $SCRIPTHOME/$FOLDERLIST
sed -e ‘s/\\/\\\\/g’ -e ‘s/(/\\(/g’ -e ‘s/)/\\)/g’ -e ‘s/{/\\{/g’ -e ‘s/}/\\}/g’ -e ‘s/\[/\\[/g’ -e ‘s/]/\\]/g’ $SCRIPTHOME/$FILESLIST
# Deleting folders created by Students from STUDENTHOME location
# Standard folders such as Desktop, Documents and so on are retained. Only their contents are deleted.
cat $SCRIPTHOME/$FOLDERLIST | while read LINE
do
if [[ “$StdFolders” == *”$LINE”* ]]
then
rm -rf $STUDENTHOME/$LINE/*
else
rm -rf — “$STUDENTHOME/$LINE”
fi
done
echo “——————>> The contents of the Standard folders have been deleted.”
echo “——————>> Except Standard folders, all other folders & their contents in $STUDENTHOME have been deleted.”
# Deleting files created by Students from STUDENTHOME location
cat $SCRIPTHOME/$FILESLIST | while read LINE
do
rm -f — “$STUDENTHOME/$LINE”
done
echo “——————>> Files in $STUDENTHOME have been deleted.”
# Cleanup of temporary files created
rm $SCRIPTHOME/$FOLDERLIST
rm $SCRIPTHOME/$FILESLIST
echo “Student account cleanup was carried out on : $TIMESTAMP” > $SCRIPTHOME/$COMPLETIONTIME
chmod 777 $SCRIPTHOME/$COMPLETIONTIME
echo “”
echo “Directories in $STUDENTHOME after deletion.”
echo “Please manually delete the files and directories that has special characters in their names.”
ls -ltr $STUDENTHOME
