Zsh
mv Foo/*(DN) Bar/
or
setopt -s glob_dots
mv Foo/*(N) Bar/
(Leave out the (N)
if you know the directory is not empty.)
Bash
shopt -s dotglob nullglob
mv Foo/* Bar/
Ksh93
If you know the directory is not empty:
FIGNORE='.?(.)'
mv Foo/* Bar/
Standard (POSIX) sh
for x in Foo/* Foo/.[!.]* Foo/..?*; do
if [ -e "$x" ]; then mv -- "$x" Bar/; fi
done
If you're willing to let the mv
command return an error status even though it succeeded, it's a lot simpler:
mv Foo/* Foo/.[!.]* Foo/..?* Bar/
GNU find and GNU mv
find Foo/ -mindepth 1 -maxdepth 1 -exec mv -t Bar/ -- {} +
Standard find
If you don't mind changing to the source directory:
cd Foo/ &&
find . -name . -o -exec sh -c 'mv -- "$@" "$0"' ../Bar/ {} + -type d -prune
Here's more detail about controlling whether dot files are matched in bash, ksh93 and zsh.
Bash
Set the dotglob
option.
$ echo *
none zero
$ shopt -s dotglob
$ echo *
..two .one none zero
There's also the more flexible GLOBIGNORE
variable, which you can set to a colon-separated list of wildcard patterns to ignore. If unset (the default setting), the shell behaves as if the value was empty if dotglob
is set, and as if the value was .*
if the option is unset. See Filename Expansion in the manual. The pervasive directories .
and ..
are always omitted, unless the .
is matched explicitly by the pattern.
$ GLOBIGNORE='n*'
$ echo *
..two .one zero
$ echo .*
..two .one
$ unset GLOBIGNORE
$ echo .*
. .. ..two .one
$ GLOBIGNORE=.:..
$ echo .*
..two .one
Ksh93
Set the FIGNORE
variable. If unset (the default setting), the shell behaves as if the value was .*
. To ignore .
and ..
, they must be matched explicitly (the manual in ksh 93s+ 2008-01-31 states that .
and ..
are always ignored, but this does not correctly describe the actual behavior).
$ echo *
none zero
$ FIGNORE='@(.|..)'
$ echo *
..two .one none zero
$ FIGNORE='n*'
$ echo *
. .. ..two .one zero
You can include dot files in a pattern by matching them explicitly.
$ unset FIGNORE
$ echo @(*|.[^.]*|..?*)
..two .one none zero
To have the expansion come out empty if the directory is empty, use the N
pattern matching option: ~(N)@(*|.[^.]*|..?*)
or ~(N:*|.[^.]*|..?*)
.
Zsh
Set the dot_glob
option.
% echo *
none zero
% setopt dot_glob
% echo *
..two .one none zero
.
and ..
are never matched, even if the pattern matches the leading .
explicitly.
% echo .*
..two .one
You can include dot files in a specific pattern with the D
glob qualifier.
% echo *(D)
..two .one none zero
Add the N
glob qualifier to make the expansion come out empty in an empty directory: *(DN)
.
Note: you may get filename expansion results in different orders
(e.g., none
followed by .one
followed by ..two
)
based on your settings of the LC_COLLATE
, LC_ALL
, and LANG
variables.
The easiest way to make a glob pattern match dot files is to use the D
glob qualifier.
**/*(D)
The precedence of ~
is lower than /
, so **~.hg/*
is **
minus the matches for .hg/*
. But **
is only special if it's before a /
, so here it matches the files in the current directory. To exclude .hg
and its contents, you need
**/*~.hg~.hg/*(D)
Note that zsh will still traverse the .hg
directory, which can take some time; this is a limitation of **
: you can't set an exclusion list directly at this level.
Best Answer
Disclaimer: This answer deals with Bash specifically but much of it applies to the question regarding glob patterns!
The star character (
*
) is a wildcard. There are a certain set of characters that it will take the place of and the first character being a dot (.
) isn't one of them. This is a special case just because of how the Unix filesystems work, files that start with a dot are considered "hidden". That means that tools such ascp
,ls
, etc. will not "see" them unless explicitly told to do so.Examples
First let's create some sample data.
So now we have the following:
Now let's play some games. You can use the command
echo
to list out what a particular wildcard (*
) would be for a given command like so:Changing the behavior?
You can use the command
shopt -s dotglob
to change the behavior of the*
so that in addition to files likeregfile1
it will also match.dotfile1
.excerpt from the
bash
man pageExample:
You can revert this behavior with this command:
Your situation?
For you you're telling
cp
that you want to copy all the files that match the pattern*
, and there aren't any files.Or you can do this if you want everything in the
foo
folder:Or you can be explicit:
A more compact form using brace expansion in
bash
:At any time you can use the
echo
trick to see what your proposed file patterns (that's the fancy term for what the star is a part of).Incidentally if you're going to copy a directory of files + other directories, you typically want to do this recursively, that's the
-R
switch tocp
: