How do I move all files in a directory (including the hidden ones) to another directory?
For example, if I have a folder "Foo" with the files ".hidden" and "notHidden" inside, how do I move both files to a directory named "Bar"? The following does not work, as the ".hidden" file stays in "Foo".
mv Foo/* Bar/
Try it yourself.
mkdir Foo
mkdir Bar
touch Foo/.hidden
touch Foo/notHidden
mv Foo/* Bar/
Best Answer
Zsh
or
(Leave out the
(N)
if you know the directory is not empty.)Bash
Ksh93
If you know the directory is not empty:
Standard (POSIX) sh
If you're willing to let the
mv
command return an error status even though it succeeded, it's a lot simpler:GNU find and GNU mv
Standard find
If you don't mind changing to the source directory:
Here's more detail about controlling whether dot files are matched in bash, ksh93 and zsh.
Bash
Set the
dotglob
option.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 ifdotglob
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.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).You can include dot files in a pattern by matching them explicitly.
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..
and..
are never matched, even if the pattern matches the leading.
explicitly.You can include dot files in a specific pattern with the
D
glob qualifier.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 theLC_COLLATE
,LC_ALL
, andLANG
variables.