Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am just curious. Is there a save guard there? . and .. is a "subdirectory" of the directory we are deleting right?

I know it's not a subdirectory but linux treat it as a file with directory property.

share|improve this question
add comment

2 Answers

up vote 19 down vote accepted

This is not a part of rm, but a part of your shell. * is a glob which your shell expands and passes to rm in the form of arguments (rm never sees a literal *, unless the glob didn't match anything, in which case a literal * is passed). Standard * globs do not expand to include filenames beginning with a dot, which includes . and ... For example:

$ tee foo bar baz .foo .bar .baz < /dev/null
$ echo *
bar baz foo
$ echo .*
. .. .bar .baz .foo
share|improve this answer
4  
In bash, shopt -s dotglob will cause dotfiles to be matched. Other shells probably have similar features. –  jordanm Jan 25 '13 at 5:19
add comment

I guess that's how 'rm' is designed.
suppose that 'rm -rf *' deletes .., then using this command on any directory will delete the entire filesystem, it's like domino effect.
why not delete ., when a user use this command inside a directory, most likely he just want to delete everything inside the directory but not the directory itself. if the dir itself gets deleted, it would surprise the user!

share|improve this answer
    
I know. So they make an exception to the rule. A good one. Is that what you're saying? –  Jim Thio Jan 25 '13 at 3:08
    
exactly. that's what I was saying. –  David Dai Jan 25 '13 at 3:09
11  
This is incorrect -- not matching files beginning with a dot is not an exception to the rule, it is the rule. rm has nothing to do with the shell's definition of what a glob is. –  Chris Down Jan 25 '13 at 3:28
    
what is a glob? WHy the downvotes? –  Jim Thio Jan 25 '13 at 3:59
1  
maybe you should delete your answer… –  taffer Jan 26 '13 at 12:24
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.