Originally published May 7, 2018 @ 10:33 am

This simplest of problems just never fails to find a new victim. For whatever reason you want to delete all hidden files in the current directory and subfolders. Without much thinking you just type rm -rf .* Here’s a classic example of one developer giving another developer a terrile advise.

The problem here, of course, is that .* wildcard expands to include .., which is the name-inode map for the parent directory. Thus, if your run rm -rf .* as root from any subfolder, you will wipe out your entire server, including the data on any network mounted shares.

Whenever deleting anything using wildcards, try to limit your destructive abilities as much as possible. For example, the command below will delete hidden files in the current directory without descenting into any subfolders:

find "$(pwd)/" -maxdepth 1 -mindepth 1 -type f -name "\.*" -delete

The following command will delete hidden folders at the current directory level:

find "$(pwd)/" -maxdepth 1 -mindepth 1 -type d -name "\.*" -exec /bin/rm -rf {} \;

You can control the depth of search by adjusting the maxdepth parameter.