9

I have a guest computer for only internet usage. Some users download some files and leave them on the desktop but the computer does not have enough space for all guests and it is not allowed to put a file in there.

What I want to do is clean every file inside the home directory when the computer starts, but keep the directory structure like Music, Pictures, Documents & Downloads.

How can I accomplish this?

3
  • To almost all: your answers - sorry - suck and you know it. What if the guest chooses another language? What about all those hidden configuration and cache files (browser history, trash, ...)? There should rather be a white list of files which won't be removed (according to Roland Taylor). Furthermore you should consider using xdg-user-dir and ~/.config/user-dirs.dirs for the default directories.
    – dAnjou
    Aug 29, 2011 at 17:06
  • @dAnjou: with my solution, the standard folders in home will be created accordingly to the choosen language. Also, the OP seems to request a clean environment, so I cannot understand why to have what you call a "white list".
    – enzotib
    Aug 29, 2011 at 19:51
  • 1
    @enzotib: Ok, sorry. Apparently Roland Taylor (and I) was wrong. I don't know what hit me. Can you edit your answer and add the possibility to put your script into /etc/gdm/PostSession or /etc/gdm/PreSession so that I can take back my downvote?
    – dAnjou
    Aug 30, 2011 at 17:16

4 Answers 4

14

You can safely remove all files in the home directory, because they are recreated, apart from files copied on user creation from /etc/skel, so

#!/bin/bash

shopt -s dotglob 
rm -rf /home/username/*
sudo -u username cp -a /etc/skel/* /home/username/

where username should be substituted with something meaningful.

Per your request ("when computer starts") this can be executed from /etc/rc.local. More likely, you want to execute it between each to logins, so put it under /etc/gdm/PostSession or /etc/gdm/PreSession.

8
  • You know there are some files that if deleted it will stop you from being able to log in?
    – RolandiXor
    Aug 29, 2011 at 15:06
  • @Roland Taylor: which file? I just tested removing all files
    – enzotib
    Aug 29, 2011 at 15:07
  • 1
    @danjjl: this option makes bash match * with all files and directories, even those hidden.
    – enzotib
    Aug 29, 2011 at 18:58
  • 1
    no, missing files will not prevent you from logging in. A missing home directory, might cause problems or files with wrong permissions set. But program should never, under any circumstances, assume that a file exists. If it doesn't exist, then it should just be created. Aug 30, 2011 at 5:25
  • 1
    I don't suggest to put it in "startup application", that are probably run after the user-dirs-update-gtk global startup script. For another user, you have to change the username in the script. Finally, guest account do not have a persistent home directory, so the script is not needed for this account.
    – enzotib
    Sep 1, 2011 at 9:13
6

You are describing the guest session. I'm not entirely sure how that works, but in 11.10 it is shown by default in the welcome screen. That account does exactly what you want so the question is if you want to spend time trying to recreate it on 11.04, or if you'd rather wait until you're comfortable with upgrading to 11.10. It's entering Beta 1 on Thursday. In any case, it doesn't hurt to download and boot a live session and see how you like it. If it's exactly the way you want it, then you'll know how to recreate it.

2

Create a script with a name of your choosing and add this content:

rm -r ~/Music/*
rm -r ~/Pictures/*
rm -r ~/Documents/*
rm -r ~/Videos/*
rm -r ~/Downloads*
rm -r ~/Desktop/*

Save this file, mark it as executable, and add it to your start up applications (for the account that needs to be cleaned).

This will clear all files in those directories. However, to clean the contents of ~/ itself is a bit more complex, and I will update my answer if I find a safe way to do that.

1

You could do something like

#!/bin/bash

rm -rf /home/Guest/Desktop/*
rm -rf /home/Guest/Documents/*
rm -rf /home/Guest/Downloads/*
rm -rf /home/Guest/Music/*
rm -rf /home/Guest/Pictures/*
rm -rf /home/Guest/Videos/*

Save this as something like cleanup.sh and make it executable with chmod +x cleanup.sh. Then you can call ./cleanup. Note that this removes everything in these folders, use with care. Be sure to change "Guest" to the name of your user.

You can make this run at startup by adding it to the Startup Applications preferences.

You could also add this script as a cron job on your system so it runs every night. See here for more information about cron jobs.

3
  • lol your answer is better than mine :D! Btw change -rf to -r as that is safer.
    – RolandiXor
    Aug 29, 2011 at 15:00
  • @Roland: not necessarily safer and even a possible pitfall: -f prevents asking for removing read-only files (files without the write bit set) and removes those without confirmation. By removing -f, a user can prevent any file from being removed by creating the /home/Guest/Desktop/000000000 file and making it read-only. This will make the script hang with a hidden confirmation and thereby not continuing with removing files. Firefox is known to make read-only downloads.
    – Lekensteyn
    Aug 29, 2011 at 15:11
  • This is why I originally included it. I didn't want the script to fail in the event of a read only file. I also figured since it was a pretty destructive script anyway, adding the -f flag wasn't going to hurt much. I may add it back in. Aug 29, 2011 at 15:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .