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.

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.)

share|improve this question

2 Answers 2

up vote 8 down vote accepted

You can use the GLOBIGNORE bash variable.

       GLOBIGNORE
              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.

and

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

So if you set

GLOBIGNORE='*/.:*/..'

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

share|improve this answer
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 .!(.|)
share|improve this answer
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

 
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.