Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I often find it annoying (or worse) when I type

command .*

in bash and the command applies to the parent directory and current directory. Is there some shell option or other configuration I can tweak to make bash exclude . and .. from the expansion of .*?

share|improve this question

2 Answers 2

up vote 10 down vote accepted

In bash, in this order:

GLOBIGNORE=.
shopt -u dotglob

When GLOBIGNORE is set, . and .. are automatically ignored, so you dont actually need to set them in GLOBIGNORE, but if you have nothing else to ignore you wont have anything to set it to.

Every time you set GLOBIGNORE to a non-null value, bash turns on the option dotglob, so you will need to turn it off afterwards; otherwise bash will expand * to include files starting with a dot.

share|improve this answer
    
Cool, thanks! I actually would rather have dotglob turned on, so setting GLOBIGNORE sounds perfect. –  David Z Aug 21 '10 at 22:04
    
When GLOBIGNORE is set, Bash does ignore . and .. but not foo/. and foo/... You may want to use GLOBIGNORE=.:*/.:*/.. to make this work with path names too. –  Pumbaa80 Aug 4 at 9:17

On an unfamiliar system where I have too little time for checking the settings in use (like GLOBIGNORE) I use the following foolproof pattern:

command .??*
  • Quick to type and guaranteed to exclude . and .. regardless of the environment.
share|improve this answer
    
I always liked this approach, but be aware that it fails to match 1-char files .a –  MestreLion Mar 28 '13 at 7:49

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.