Accidentally Deleting Directories

I suppose every seasoned Linux user has been there (I know I have): done “rm -rf” (or equivalent) to wipe out a directory, only to realize--too late--that it was the wrong one. There’s a discussion currently raging <http://www.theregister.co.uk/2015/01/17/scary_code_of_the_week_steam_cleans_linux_pcs/> over a bug just like this in the Steam client for Linux. There is a script that contains the line rm -rf "$STEAMROOT/"* However, as a result of certain (unusual) user actions, the value of the STEAMROOT variable could end up empty, in which case the script proceeds to wipe out every file on the system that your userid has privilege over. What’s the best way to handle this? On the one hand, when setting up a new installation/preferences directory, you want to ensure there is no leftover rubbish in it. But on the other, you want to ensure that any existing directory you delete/replace is in fact an old installation/preferences directory. I’m leaning towards the solution--discussed in the GitHub issue entry--of checking for some distinctive file in the existing directory. If that doesn’t exist, then the directory is not what the script thinks it is. For example, my wipetree script <https://github.com/ldo/git-useful/blob/master/wipetree> checks that there is a .git subdirectory in the directory you want to wipe. It also helps to use “set -e”, so bash aborts the script on an error, instead of barrelling on.

On Sun, 18 Jan 2015 16:56:37 Lawrence D'Oliveiro wrote:
There’s a discussion currently raging <http://www.theregister.co.uk/2015/01/17/scary_code_of_the_week_steam_cleans _linux_pcs/> over a bug just like this in the Steam client for Linux. There is a script that contains the line
rm -rf "$STEAMROOT/"*
However, as a result of certain (unusual) user actions, the value of the STEAMROOT variable could end up empty, in which case the script proceeds to wipe out every file on the system that your userid has privilege over.
What’s the best way to handle this?
if [ -d "$STEAMROOT" ] ; then rm -rf "$STEAMROOT/"* else echo Steamroot directory "$STEAMROOT" does not exist or is not a directory exit 1 fi Wayne
participants (2)
-
Lawrence D'Oliveiro
-
Wayne Rooney