Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 10 down vote accepted

In bash, in this order:

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. – user123444555621 Aug 4 '14 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .