Bash – Setting for Globbing to Control * Matching Dot Files

bashwildcards

I was surprised recently when I did something like
mv ./* ../somedirectory
and found that files like .gitignore were not moved.

I do most of my work in zsh on OS X, and this surprise bit me in bash on CentOS. I tried bash on OS X and found the same behavior: * does not match dot files. This seems very undesirable to me, but apparently it's the bash default. (It may be the zsh default too for all I remember, but I may have changed it years ago in my .zshrc and forgotten it ever worked differently.)

How can I configure bash to behave as I expected: for * to match all files, and not ignore dot files.

In case this is at all unclear, here's how to reproduce it

cd /tmp
mkdir {t,d}est
touch test/{.,}{1,2,3,4,5,6,7}
ls -hal test
mv test/* dest
ls -hal test     # notice dot files are still there
ls -hal dest     # notice only some files were mv'ed

Best Answer

Bash

As you already noticed bash won't match a . at the start of the name or a slash. To change the matching regarding the dot you have to set the dotglob option - man bash:

dotglob If set, bash includes filenames beginning with a `.'  in
    the results of pathname expansion.

To enable/set it with bash use shopt, e.g:

shopt -s dotglob


For zsh you can also use the dotglob option but but you will have to use setopt to enable it, e.g:

setopt dotglob