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.

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
share|improve this question
    
    
Yes, they're related. That didn't show up when I searched (probably because the questioner of that didn't mention globbing or dot files), but it's really a different question. He was asking how to move the files, and I was asking how to change the shell behavior, and specifically for bash. I might not have asked if that question had shown up for me (since your extremely complete and thorough answer there includes the bash setting) but it's still a different question, and someone looking for an answer to my question isn't necessarily going to find his question. –  iconoclast Jun 18 '12 at 14:41
    
That whole “someone looking for an answer to my question isn't necessarily going to find his question” business is exactly why we have this way of closing questions as duplicates. –  Gilles Jun 18 '12 at 22:05
add comment

2 Answers

up vote 8 down vote accepted

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
share|improve this answer
add comment

I tested this and it solves the issue:

shopt -s dotglob

Output:

~/stackexchangeanswers/40662$ ls -hal dest
total 8.0K
drwxr-xr-x 2 jodiec jodiec 4.0K 2012-06-12 22:15 .
drwxr-xr-x 4 jodiec jodiec 4.0K 2012-06-12 22:15 ..
-rw-r--r-- 1 jodiec jodiec    0 2012-06-12 22:15 1
-rw-r--r-- 1 jodiec jodiec    0 2012-06-12 22:15 .1
-rw-r--r-- 1 jodiec jodiec    0 2012-06-12 22:15 2
-rw-r--r-- 1 jodiec jodiec    0 2012-06-12 22:15 .2
-rw-r--r-- 1 jodiec jodiec    0 2012-06-12 22:15 3
-rw-r--r-- 1 jodiec jodiec    0 2012-06-12 22:15 .3
...snipped....
share|improve this answer
    
Sorry but Ulrich answered first. –  iconoclast Jun 13 '12 at 3:44
1  
We're all on the same team. It's ok. –  JodieC Jun 13 '12 at 4:46
add comment

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.