Ubuntu – How to write a small script to clean every directory’s files when the computer starts

command linesoftware-recommendation

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?

Best Answer

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.