I'm helping to teach two Unix courses next week. Users will be granted an account on a RHEL 5 machine, during which time they'll add files to their /home folder, update their .bashrc and other dotfiles, and perform other general messiness that needs to be cleaned up.

Students in the second session will be reusing the user accounts of the folks in the first session. I'd like to automate the cleanup of their accounts, so new users can start fresh.

I'm sure I could write a shell script that runs a su -u $USER_ID and lays down an "original" set of good files in their homedir, and removes anything else it finds. Are there other tools that will help me with this clean/reset operation? I don't have any experience with Puppet, Chef, or other tools. Would they be helpful to do something like this?

To provide a scope of the problem, there's about 30 user accounts, I know all the userid/passwords, and they're all created on the same RHEL box.

  • If you teach it you'll probably know it already, but Linux is not Unix. The name comes from Linus, but it's also an acronym for [L]inux [i]s [n]ot [U]ni[x]. – mailq Sep 10 '11 at 16:05
  • True enough. I've changed the title to reflect it. – matthewsteele Sep 14 '11 at 16:10

There are many ways that would help:

  • remove the complete home directory and copy all files from /etc/skel back to the homedir. Change permissions afterwards.
  • put the system in a virtual machine, make a snapshot and revert to the snapshot after lesson 1
  • find something like a kiosk mode in RHEL. Ubuntu has something like that, which automatically restores the home during logoff
  • put the home on a btrfs filesystem, make a snapshot and revert after lesson 1
  • tar the home dir before lesson, delete home afterwards, restore from tar
  • ...

And learning other tools like Puppet/Chef is a little bit too much if you want results next week.

  • Thanks. I'll probably run a tar and restore, and try learning a tool like Puppet when I'm not under a time crunch. – matthewsteele Sep 8 '11 at 13:16

Lets say all your students had a UID between 1000 and 65000

A quick one-liner like this would work. Awk will print out an rsync command and chown command for every user in the range. Sending the output of the awk into bash will run the all the commands to reset directories and permissions.

# see what will happen.
awk 'BEGIN{FS=":"} $3 >= 1000 && $3 <=65000 { print "rsync --delete -v -r /etc/skel/ " $6 "/ ; chown -R " $1 ":" $1 " " $6;}' /etc/passwd

# actually run the commands to reset all users matched by awk.
bash <( awk 'BEGIN{FS=":"} $3 >= 1000 && $3 <=65000 { print "rsync --delete -v -r /etc/skel/ " $6 "/ ; chown -R " $1 ":" $1 " " $6;}' /etc/passwd )

If you're using gdm for your login manager you can add something like this file: /etc/gdm/PostSession/Default


if [[ "$USER" != "" ]]; then
   rm -rf /home/$USER
   cp -r /etc/skel /home/$USER
   chown -R $USER:$USER /home/$USER

The "original" set of account files are usually located under /etc/skel in unix systems.

Apart from this, I don't know any tool to automate the cleanup. I'd probably end up writing some simple bash script.


Try this



# error codes

function handle_error


# INV means to invert the handling logic

if [ -z $INV ]; then
  if [ $CODE -ne 0 ]; then
    echo "error: $ACTION"
    exit $E_NOK
 if [ $CODE -eq 0 ]; then
    echo "error: $ACTION"
    exit $E_NOK

  return $E_OK

function print_usage()

  echo "usage: reset-homedir.sh USERNAME"

# target user and target dir

if [ -z $TUSER ]; then
  exit 0

getent passwd $TUSER >& /dev/null
handle_error $RC "user $TUSER does not exist"

TGROUP="`id -gn $1`"

if [ ! -d $TDIR ]; then
  echo "error: target directory $TDIR does not exist"
  exit 1

# you don't want to delete user mounted stuff do you?
MOUNTS="`mount |grep $TDIR`" >& /dev/null
handle_error $RC "there are mounted filesystems below $TDIR" TRUE

ps -u $TUSER >& /dev/null
handle_error $RC "user $TUSER is logged in" TRUE

echo "$TDIR will be reset to the default state - ALL DATA WILL BE LOST"
echo "-- press ENTER to continue or CTRL+C to abort --"
read dummy

# we did our best to check for unwanted situations
rm -rf $TDIR
# creates a new one
cp -R /etc/skel $TDIR

echo Done.

There may be some nice tool that will help, but I would probably just (a) put the desired stuff in a git repo, and (b) write a script which iterates over the users and rm's the desired stuff and pulls from the git repo.


This works for Ubuntu gnome: pico /usr/local/bin/cleanup.sh

rm -rf /home/user/Downloads/*

do the same if you want to remove Documents

exit chmod 511 /usr/local/bin/cleanup.sh

pico /etc/gdm/PostSession/Default


/usr/local/bin/cleanup.sh exit 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.