When I try to match all dot files in a directory with .* it seems to have a nasty side-effect: besides matching all (real) files and directories, it matches . and ...

bash-3.2$ mv test/.* dest/
mv: rename test/. to dest/.: Invalid argument
mv: test/.. and dest/.. are identical

This seems really weird, since they are basically 'fake' directories, just there to make relative paths easy. They are not part of the contents of a directory, and I don't ever want them matched when I try to move the contents of one directory to another directory. I can't think of any scenario where I would want them matched by .*.

So how can I turn this off? (Besides using Z shell, which is not always available, and which may not be the shell in use by someone running a function I've written.)


You can use the GLOBIGNORE bash variable.

              A colon-separated list of patterns defining the set of filenames
              to be ignored by pathname expansion.  If a filename matched by a
              pathname  expansion  pattern also matches one of the patterns in
              GLOBIGNORE, it is removed from the list of matches.


       .......................-.  The file names ``.''  and ``..''  are always
       ignored  when GLOBIGNORE is set and not null.  

So if you set


then path/.* will not match . and .., as you ask.

  • 2
    Wouldn't GLOBIGNORE=.:.. more succinctly accomplish the same thing? Or is there a subtle difference I'm missing? – iconoclast Jun 18 '12 at 14:50

A glob pattern would be .[^.]* -- it matches something with at least 2 characters where the 2nd char is not a dot.

Documentation for filename pattern matching is in the bash man page or here: http://www.gnu.org/software/bash/manual/bash.html#Pattern-Matching

Jonathan Callen raises a good point. You can use a fairly simple extended pattern

shopt -s extglob
ls -d .!(.|)
  • 1
    That will miss files named like ..install.cmd (A real file I've seen in the wild). – Jonathan Callen Jun 13 '12 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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