At the Manitoba Unix Users Group last night, a common problem was presented as a warning to people new to unix based operating systems. The message was “All operating systems will let you shoot yourself in the foot. Unix based systems will let you shoot yourself in the foot really fast.” One of the examples was the dangers of using root while executing recursive delete commands.
Say you wanted to get rid of all the hidden files in your directory (those starting with . for example .htaccess and .htpasswd)
DO NOT USE “rm -Rf .*” AS ROOT and be careful as a normal user!
Whatever you do, don’t execute that as root unless you really do want to delete everything on that filesystem. Even as a normal user it can be dangerous because it’ll delete as far back as you have permissions to go. Why?
Do a quick “ls -a” in any directory. The first 2 lines are the key. Here’s the contents of my /home/ directory:
.
..
.directory
.test
chris
Assume .directory is a directory, and .test is a test, and I wanted to delete both with one command. Why does “rm -Rf .*” delete everything in that directory? “.*” matches “.directory”, “.test”, but it also matches “.” which means current directory. So you’re telling it to recursively (-R) and forcefully (-f deletes write protected files too) delete the entire /home/ directory. What’s worse, is “.*” also matches “..” which means parent directory, so you’re telling it to also recursively delete the parent directory. Which repeats all the way back till you’ve got nothing left!
You can still do what you intended, but leaving the current directory’s visible contents and any parent directories intact. “rm -Rf .??*”.
“.??” matches the first letter of the hidden directories, and ?? ensures that at least two characters follow. That keeps it from matching both “.” and “..”.
Problem solved! Remember, when you’re working in a unix console, be very careful of what you’re typing when you’re executing destructive commands. “.??*” is only one character away from it deleting everything! And to limit any damage that a fat finger episode might accidentally cause, don’t use the root user unless you really have to.



Chris, I’ve got to remember this.