Jump to content

Recommended Posts

Posted

I thought i would share this script below, i cant take any credit for it as it was provided by Jamf

 

we was in a pickle where by a lot of our pupils where saving items to the desktop from garage band and basically bad house keeping by the pupil, which caused some issues where when the macs wanted to update they couldn't due to not having storage avliable to complete the update. 

 

so jamf provided me the script below that basically deletes users from the user directory. 

 

hopefully this will help you as it has me, PROTECTED_USERS=("xxxxxxx") this part of the script excludes any admin account / user you have which you don't want the script to run against

 

 

#!/bin/bash

PROTECTED_USERS=("xxxxxxx")

LOG_FILE="/var/log/student_cleanup.log"

log() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}

is_protected() {
    local user="$1"
    for protected in "${PROTECTED_USERS[@]}"; do
        [[ "$user" == "$protected" ]] && return 0
    done
    return 1
}

log "========== Cleanup Started =========="

for USER_DIR in /Users/*/; do
    USERNAME=$(basename "$USER_DIR")

    if is_protected "$USERNAME"; then
        log "SKIPPED (protected): $USERNAME"
        continue
    fi

    if who | awk '{print $1}' | grep -q "^${USERNAME}$"; then
        log "SKIPPED (logged in): $USERNAME"
        continue
    fi

    log "Cleaning: $USERNAME"

    rm -rf "$USER_DIR/Desktop/"*
    rm -rf "$USER_DIR/Downloads/"*
    rm -rf "$USER_DIR/.Trash/"*

    log "Done: $USERNAME"
done

log "========== Cleanup Finished =========="

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...