How do you move all files (including hidden) from one directory to another?
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/
shell wildcards dot-files mv
migrated from stackoverflow.com Jan 24 '11 at 19:33
This question came from our site for professional and enthusiast programmers.
add a comment |
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/
shell wildcards dot-files mv
migrated from stackoverflow.com Jan 24 '11 at 19:33
This question came from our site for professional and enthusiast programmers.
Linux: How to move all files from current directory to upper directory ?
– Andrew T Finnell
Jan 24 '11 at 19:36
Bah, I knew I should have refreshed the answers before typing mine up. Very helpful link.
– Steven D
Jan 24 '11 at 19:48
Sadly, this question ended up here because I said on SO that it should move here or SU, so of course it moved here when there was a duplicate on SU already :)
– Michael Mrozek♦
Jan 24 '11 at 20:06
Personally, I think *nix specific questions and answers should be off-topic on SU. With the current rules we end up with tons of content collisions between the two - like this question.
– Cory Klein
May 12 '14 at 17:27
add a comment |
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/
shell wildcards dot-files mv
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/
shell wildcards dot-files mv
shell wildcards dot-files mv
edited Apr 29 '15 at 16:30
Cory Klein
asked Jan 24 '11 at 19:18
Cory KleinCory Klein
5,512215984
5,512215984
migrated from stackoverflow.com Jan 24 '11 at 19:33
This question came from our site for professional and enthusiast programmers.
migrated from stackoverflow.com Jan 24 '11 at 19:33
This question came from our site for professional and enthusiast programmers.
Linux: How to move all files from current directory to upper directory ?
– Andrew T Finnell
Jan 24 '11 at 19:36
Bah, I knew I should have refreshed the answers before typing mine up. Very helpful link.
– Steven D
Jan 24 '11 at 19:48
Sadly, this question ended up here because I said on SO that it should move here or SU, so of course it moved here when there was a duplicate on SU already :)
– Michael Mrozek♦
Jan 24 '11 at 20:06
Personally, I think *nix specific questions and answers should be off-topic on SU. With the current rules we end up with tons of content collisions between the two - like this question.
– Cory Klein
May 12 '14 at 17:27
add a comment |
Linux: How to move all files from current directory to upper directory ?
– Andrew T Finnell
Jan 24 '11 at 19:36
Bah, I knew I should have refreshed the answers before typing mine up. Very helpful link.
– Steven D
Jan 24 '11 at 19:48
Sadly, this question ended up here because I said on SO that it should move here or SU, so of course it moved here when there was a duplicate on SU already :)
– Michael Mrozek♦
Jan 24 '11 at 20:06
Personally, I think *nix specific questions and answers should be off-topic on SU. With the current rules we end up with tons of content collisions between the two - like this question.
– Cory Klein
May 12 '14 at 17:27
Linux: How to move all files from current directory to upper directory ?
– Andrew T Finnell
Jan 24 '11 at 19:36
Linux: How to move all files from current directory to upper directory ?
– Andrew T Finnell
Jan 24 '11 at 19:36
Bah, I knew I should have refreshed the answers before typing mine up. Very helpful link.
– Steven D
Jan 24 '11 at 19:48
Bah, I knew I should have refreshed the answers before typing mine up. Very helpful link.
– Steven D
Jan 24 '11 at 19:48
Sadly, this question ended up here because I said on SO that it should move here or SU, so of course it moved here when there was a duplicate on SU already :)
– Michael Mrozek♦
Jan 24 '11 at 20:06
Sadly, this question ended up here because I said on SO that it should move here or SU, so of course it moved here when there was a duplicate on SU already :)
– Michael Mrozek♦
Jan 24 '11 at 20:06
Personally, I think *nix specific questions and answers should be off-topic on SU. With the current rules we end up with tons of content collisions between the two - like this question.
– Cory Klein
May 12 '14 at 17:27
Personally, I think *nix specific questions and answers should be off-topic on SU. With the current rules we end up with tons of content collisions between the two - like this question.
– Cory Klein
May 12 '14 at 17:27
add a comment |
10 Answers
10
active
oldest
votes
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.
1
Whynullglob
? It would make more sense to abort themv
command (like fish, csh/tcsh, zsh (without(N)
) do, or bash with failglob) than running amv /Bar/
command which makes little sense.
– Stéphane Chazelas
Feb 21 '16 at 16:29
1
I'd say your GLOBIGNORE description is inaccurate and misleading. Setting GLOBIGNORE to a non-empty value turns on dotglob and makes that.
and..
are never matched (like in other sensible shells like zsh, fish, pdksh and derivatives) even if you turn dotglob back off afterwards. (GLOBIGNORE=:; shopt -u dotglob; echo .*
won't output.
and..
). setting GLOBIGNORE to.:..
or.
or:
have the same effect.
– Stéphane Chazelas
Feb 21 '16 at 19:37
ksh93
always ignoring.
and..
seems to have been broken in betweenksh93k+
(where it worked) andksh93m
(where it no longer works). Note that it's bad in thatksh93
will take the value of FIGNORE from the environment, so aFIGNORE='!(..)'
env var for instance can create havoc.
– Stéphane Chazelas
Feb 21 '16 at 21:05
add a comment |
#!/bin/bash
shopt -s dotglob
mv Foo/* Bar/
From man bash
dotglob If set, bash includes
filenames beginning with a '.' in the
results of pathname expansion.
With the caveat that this command returns an error code if the directory was empty (even though the command actually performed as intended).
– Gilles
Jan 24 '11 at 20:28
1
@Gilles, the error will then be frommv
complaining that that file calledFoo/*
doesn't exist. Interestingly, ifFoo
is searchable, but not readable, and there is a file called*
in there, you'll get no error, that file will be moved, but not the other ones in the directory.bash
has afailglob
option so it behaves more likezsh
,fish
orcsh
/tcsh
and abort the command with an error when a glob cannot be expanded instead of that bogus behaviour of leaving the glob as-is.
– Stéphane Chazelas
Feb 22 '16 at 10:01
add a comment |
A simple way to do this in bash
is
mv {Foo/*,Foo/.*} Bar/
But this will also move directories.
If you want to move all files including hidden but don't want to move any directory you can use a for loop and test.
for i in $(ls -d {Foo/*,Foo/.*});do test -f $i && mv -v $i Bar/; done;
add a comment |
One way is to use find
:
find Foo/ -type f -exec mv -t Bar/ {} +
The -type f
restricts the find command to finding files. You should investigate the -type
, -maxdepth
, and -mindepth
options of find
to customize your command to account for subdirectories. Find has a lengthy but very helpful manual page.
3
That only moves the regular files inFoo/
, not subdirectories and other files.
– Gilles
Jan 24 '11 at 20:29
add a comment |
Try the copy command cp
:
$ cp -r myfolder/* destinationfolder
cp -r
means copy recursive, so all folders and files will be copied.
You can use the remove command rm
to remove a folder:
$ rm -r myfolder
See more: move all files from a directory to another one.
2
Not the best when you're moving a lot of large files, but I guess it would work.
– Cory Klein
May 12 '14 at 17:25
add a comment |
Rsync is another option:
rsync -axvP --remove-source-files sourcedirectory/ targetdirectory
This works because in rsync the trailing slash matters, sourcedirectory/
refers to the content of the directory, while sourcedirectory
would refer to the directory itself.
The disadvantage of this method is that rsync will only cleanup the files after the move, not the directory. So you are left with an empty sourcedirectory tree. For workarounds for that, see:
Move files and delete directories with rsync?
So while this might not be optimal for move operations, it can be extremely useful for copy operations.
add a comment |
Answer for bash/fish
Here's a way to do it using wildcards:
.[!.]* ..?*
will match all hidden files except .
and ..
.[!.]* ..?* *
will match all files (hidden or not) except .
and ..
And to answer the particular example of this question you need cd foo && mv .[!.]* ..?* * ../bar
Explanation
.[!.]*
matches file-names starting with one dot, followed by any character except the dot optionally followed by any string. This is close enough but it misses files starting with two dots like ..foo
. To include such files we add ..?*
which matches file-names starting with two dots, followed by any character, optionally followed by any string.
Test
You can test these wildcards with the commands below. I've tried them successfully under bash and fish. They fail under sh, zsh, xonsh.
mkdir temp
cd temp
touch a .b .bc ..c ..cd ...d ...de
ls .[!.]* ..?*
# you get this output:
.b .bc ..c ..cd ...d ...de
# cleanup
cd ..
rm -rf temp
add a comment |
You might also be able to find and grep with backquotes to select files for the move command. Pass those into mv.
I.e. For hidden files
find Foo -maxdepth 1 | egrep '^Foo/[.]' # Output: .hidden
So
mv `find Foo -maxdepth 1 | egrep '^Foo/[.]'` Bar # mv Foo/.hidden Bar
Moves only selected hidden files into Bar
:
mv `find Foo -maxdepth 1 | egrep '^Foo/.'` Bar # mv Foo/.hidden Foo/notHidden Bar
Moves all files in Foo to Bar since the '.' in the egrep command acts as a wildcard without the square brackets.
The ^
character ensures the match starts from the beginning of the line.
Some details of egrep
pattern matching can be found here.
Using maxdepth 1
stops find from going into subdirectories.
add a comment |
Inspired from this answer:
Without copying the files...
rsync -ax --link-dest=../Foo/ Foo/ Bar
Caveats:
--link-dest
path must be absolute or relative to DESTINATION (Bar
in the example)You've to put
/
after SOURCE (Foo/
in the example), otherwise it
will copy the SOURCE folder instead of contents of it.
add a comment |
I find that this works well for bash and there's no need to change shell options
mv sourcedir/{*,.[^.]*} destdir/
New contributor
This basically duplicates ndemou’s answer except you added curly braces (without explaining them). But (1) your answer doesn’t match files whose names begin with..
, and (2) to be POSIX compliant, you should use!
instead of^
; i.e.,{*,.[!.]*}
.
– G-Man
9 mins ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f6393%2fhow-do-you-move-all-files-including-hidden-from-one-directory-to-another%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
1
Whynullglob
? It would make more sense to abort themv
command (like fish, csh/tcsh, zsh (without(N)
) do, or bash with failglob) than running amv /Bar/
command which makes little sense.
– Stéphane Chazelas
Feb 21 '16 at 16:29
1
I'd say your GLOBIGNORE description is inaccurate and misleading. Setting GLOBIGNORE to a non-empty value turns on dotglob and makes that.
and..
are never matched (like in other sensible shells like zsh, fish, pdksh and derivatives) even if you turn dotglob back off afterwards. (GLOBIGNORE=:; shopt -u dotglob; echo .*
won't output.
and..
). setting GLOBIGNORE to.:..
or.
or:
have the same effect.
– Stéphane Chazelas
Feb 21 '16 at 19:37
ksh93
always ignoring.
and..
seems to have been broken in betweenksh93k+
(where it worked) andksh93m
(where it no longer works). Note that it's bad in thatksh93
will take the value of FIGNORE from the environment, so aFIGNORE='!(..)'
env var for instance can create havoc.
– Stéphane Chazelas
Feb 21 '16 at 21:05
add a comment |
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.
1
Whynullglob
? It would make more sense to abort themv
command (like fish, csh/tcsh, zsh (without(N)
) do, or bash with failglob) than running amv /Bar/
command which makes little sense.
– Stéphane Chazelas
Feb 21 '16 at 16:29
1
I'd say your GLOBIGNORE description is inaccurate and misleading. Setting GLOBIGNORE to a non-empty value turns on dotglob and makes that.
and..
are never matched (like in other sensible shells like zsh, fish, pdksh and derivatives) even if you turn dotglob back off afterwards. (GLOBIGNORE=:; shopt -u dotglob; echo .*
won't output.
and..
). setting GLOBIGNORE to.:..
or.
or:
have the same effect.
– Stéphane Chazelas
Feb 21 '16 at 19:37
ksh93
always ignoring.
and..
seems to have been broken in betweenksh93k+
(where it worked) andksh93m
(where it no longer works). Note that it's bad in thatksh93
will take the value of FIGNORE from the environment, so aFIGNORE='!(..)'
env var for instance can create havoc.
– Stéphane Chazelas
Feb 21 '16 at 21:05
add a comment |
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.
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.
edited May 1 '16 at 6:46
user79743
answered Jan 24 '11 at 20:25
GillesGilles
540k12810941609
540k12810941609
1
Whynullglob
? It would make more sense to abort themv
command (like fish, csh/tcsh, zsh (without(N)
) do, or bash with failglob) than running amv /Bar/
command which makes little sense.
– Stéphane Chazelas
Feb 21 '16 at 16:29
1
I'd say your GLOBIGNORE description is inaccurate and misleading. Setting GLOBIGNORE to a non-empty value turns on dotglob and makes that.
and..
are never matched (like in other sensible shells like zsh, fish, pdksh and derivatives) even if you turn dotglob back off afterwards. (GLOBIGNORE=:; shopt -u dotglob; echo .*
won't output.
and..
). setting GLOBIGNORE to.:..
or.
or:
have the same effect.
– Stéphane Chazelas
Feb 21 '16 at 19:37
ksh93
always ignoring.
and..
seems to have been broken in betweenksh93k+
(where it worked) andksh93m
(where it no longer works). Note that it's bad in thatksh93
will take the value of FIGNORE from the environment, so aFIGNORE='!(..)'
env var for instance can create havoc.
– Stéphane Chazelas
Feb 21 '16 at 21:05
add a comment |
1
Whynullglob
? It would make more sense to abort themv
command (like fish, csh/tcsh, zsh (without(N)
) do, or bash with failglob) than running amv /Bar/
command which makes little sense.
– Stéphane Chazelas
Feb 21 '16 at 16:29
1
I'd say your GLOBIGNORE description is inaccurate and misleading. Setting GLOBIGNORE to a non-empty value turns on dotglob and makes that.
and..
are never matched (like in other sensible shells like zsh, fish, pdksh and derivatives) even if you turn dotglob back off afterwards. (GLOBIGNORE=:; shopt -u dotglob; echo .*
won't output.
and..
). setting GLOBIGNORE to.:..
or.
or:
have the same effect.
– Stéphane Chazelas
Feb 21 '16 at 19:37
ksh93
always ignoring.
and..
seems to have been broken in betweenksh93k+
(where it worked) andksh93m
(where it no longer works). Note that it's bad in thatksh93
will take the value of FIGNORE from the environment, so aFIGNORE='!(..)'
env var for instance can create havoc.
– Stéphane Chazelas
Feb 21 '16 at 21:05
1
1
Why
nullglob
? It would make more sense to abort the mv
command (like fish, csh/tcsh, zsh (without (N)
) do, or bash with failglob) than running a mv /Bar/
command which makes little sense.– Stéphane Chazelas
Feb 21 '16 at 16:29
Why
nullglob
? It would make more sense to abort the mv
command (like fish, csh/tcsh, zsh (without (N)
) do, or bash with failglob) than running a mv /Bar/
command which makes little sense.– Stéphane Chazelas
Feb 21 '16 at 16:29
1
1
I'd say your GLOBIGNORE description is inaccurate and misleading. Setting GLOBIGNORE to a non-empty value turns on dotglob and makes that
.
and ..
are never matched (like in other sensible shells like zsh, fish, pdksh and derivatives) even if you turn dotglob back off afterwards. (GLOBIGNORE=:; shopt -u dotglob; echo .*
won't output .
and ..
). setting GLOBIGNORE to .:..
or .
or :
have the same effect.– Stéphane Chazelas
Feb 21 '16 at 19:37
I'd say your GLOBIGNORE description is inaccurate and misleading. Setting GLOBIGNORE to a non-empty value turns on dotglob and makes that
.
and ..
are never matched (like in other sensible shells like zsh, fish, pdksh and derivatives) even if you turn dotglob back off afterwards. (GLOBIGNORE=:; shopt -u dotglob; echo .*
won't output .
and ..
). setting GLOBIGNORE to .:..
or .
or :
have the same effect.– Stéphane Chazelas
Feb 21 '16 at 19:37
ksh93
always ignoring .
and ..
seems to have been broken in between ksh93k+
(where it worked) and ksh93m
(where it no longer works). Note that it's bad in that ksh93
will take the value of FIGNORE from the environment, so a FIGNORE='!(..)'
env var for instance can create havoc.– Stéphane Chazelas
Feb 21 '16 at 21:05
ksh93
always ignoring .
and ..
seems to have been broken in between ksh93k+
(where it worked) and ksh93m
(where it no longer works). Note that it's bad in that ksh93
will take the value of FIGNORE from the environment, so a FIGNORE='!(..)'
env var for instance can create havoc.– Stéphane Chazelas
Feb 21 '16 at 21:05
add a comment |
#!/bin/bash
shopt -s dotglob
mv Foo/* Bar/
From man bash
dotglob If set, bash includes
filenames beginning with a '.' in the
results of pathname expansion.
With the caveat that this command returns an error code if the directory was empty (even though the command actually performed as intended).
– Gilles
Jan 24 '11 at 20:28
1
@Gilles, the error will then be frommv
complaining that that file calledFoo/*
doesn't exist. Interestingly, ifFoo
is searchable, but not readable, and there is a file called*
in there, you'll get no error, that file will be moved, but not the other ones in the directory.bash
has afailglob
option so it behaves more likezsh
,fish
orcsh
/tcsh
and abort the command with an error when a glob cannot be expanded instead of that bogus behaviour of leaving the glob as-is.
– Stéphane Chazelas
Feb 22 '16 at 10:01
add a comment |
#!/bin/bash
shopt -s dotglob
mv Foo/* Bar/
From man bash
dotglob If set, bash includes
filenames beginning with a '.' in the
results of pathname expansion.
With the caveat that this command returns an error code if the directory was empty (even though the command actually performed as intended).
– Gilles
Jan 24 '11 at 20:28
1
@Gilles, the error will then be frommv
complaining that that file calledFoo/*
doesn't exist. Interestingly, ifFoo
is searchable, but not readable, and there is a file called*
in there, you'll get no error, that file will be moved, but not the other ones in the directory.bash
has afailglob
option so it behaves more likezsh
,fish
orcsh
/tcsh
and abort the command with an error when a glob cannot be expanded instead of that bogus behaviour of leaving the glob as-is.
– Stéphane Chazelas
Feb 22 '16 at 10:01
add a comment |
#!/bin/bash
shopt -s dotglob
mv Foo/* Bar/
From man bash
dotglob If set, bash includes
filenames beginning with a '.' in the
results of pathname expansion.
#!/bin/bash
shopt -s dotglob
mv Foo/* Bar/
From man bash
dotglob If set, bash includes
filenames beginning with a '.' in the
results of pathname expansion.
answered Jan 24 '11 at 19:38
SiegeXSiegeX
5,46112723
5,46112723
With the caveat that this command returns an error code if the directory was empty (even though the command actually performed as intended).
– Gilles
Jan 24 '11 at 20:28
1
@Gilles, the error will then be frommv
complaining that that file calledFoo/*
doesn't exist. Interestingly, ifFoo
is searchable, but not readable, and there is a file called*
in there, you'll get no error, that file will be moved, but not the other ones in the directory.bash
has afailglob
option so it behaves more likezsh
,fish
orcsh
/tcsh
and abort the command with an error when a glob cannot be expanded instead of that bogus behaviour of leaving the glob as-is.
– Stéphane Chazelas
Feb 22 '16 at 10:01
add a comment |
With the caveat that this command returns an error code if the directory was empty (even though the command actually performed as intended).
– Gilles
Jan 24 '11 at 20:28
1
@Gilles, the error will then be frommv
complaining that that file calledFoo/*
doesn't exist. Interestingly, ifFoo
is searchable, but not readable, and there is a file called*
in there, you'll get no error, that file will be moved, but not the other ones in the directory.bash
has afailglob
option so it behaves more likezsh
,fish
orcsh
/tcsh
and abort the command with an error when a glob cannot be expanded instead of that bogus behaviour of leaving the glob as-is.
– Stéphane Chazelas
Feb 22 '16 at 10:01
With the caveat that this command returns an error code if the directory was empty (even though the command actually performed as intended).
– Gilles
Jan 24 '11 at 20:28
With the caveat that this command returns an error code if the directory was empty (even though the command actually performed as intended).
– Gilles
Jan 24 '11 at 20:28
1
1
@Gilles, the error will then be from
mv
complaining that that file called Foo/*
doesn't exist. Interestingly, if Foo
is searchable, but not readable, and there is a file called *
in there, you'll get no error, that file will be moved, but not the other ones in the directory. bash
has a failglob
option so it behaves more like zsh
, fish
or csh
/tcsh
and abort the command with an error when a glob cannot be expanded instead of that bogus behaviour of leaving the glob as-is.– Stéphane Chazelas
Feb 22 '16 at 10:01
@Gilles, the error will then be from
mv
complaining that that file called Foo/*
doesn't exist. Interestingly, if Foo
is searchable, but not readable, and there is a file called *
in there, you'll get no error, that file will be moved, but not the other ones in the directory. bash
has a failglob
option so it behaves more like zsh
, fish
or csh
/tcsh
and abort the command with an error when a glob cannot be expanded instead of that bogus behaviour of leaving the glob as-is.– Stéphane Chazelas
Feb 22 '16 at 10:01
add a comment |
A simple way to do this in bash
is
mv {Foo/*,Foo/.*} Bar/
But this will also move directories.
If you want to move all files including hidden but don't want to move any directory you can use a for loop and test.
for i in $(ls -d {Foo/*,Foo/.*});do test -f $i && mv -v $i Bar/; done;
add a comment |
A simple way to do this in bash
is
mv {Foo/*,Foo/.*} Bar/
But this will also move directories.
If you want to move all files including hidden but don't want to move any directory you can use a for loop and test.
for i in $(ls -d {Foo/*,Foo/.*});do test -f $i && mv -v $i Bar/; done;
add a comment |
A simple way to do this in bash
is
mv {Foo/*,Foo/.*} Bar/
But this will also move directories.
If you want to move all files including hidden but don't want to move any directory you can use a for loop and test.
for i in $(ls -d {Foo/*,Foo/.*});do test -f $i && mv -v $i Bar/; done;
A simple way to do this in bash
is
mv {Foo/*,Foo/.*} Bar/
But this will also move directories.
If you want to move all files including hidden but don't want to move any directory you can use a for loop and test.
for i in $(ls -d {Foo/*,Foo/.*});do test -f $i && mv -v $i Bar/; done;
edited Sep 8 '13 at 23:21
answered Aug 11 '13 at 19:34
Boris MonthBoris Month
5113
5113
add a comment |
add a comment |
One way is to use find
:
find Foo/ -type f -exec mv -t Bar/ {} +
The -type f
restricts the find command to finding files. You should investigate the -type
, -maxdepth
, and -mindepth
options of find
to customize your command to account for subdirectories. Find has a lengthy but very helpful manual page.
3
That only moves the regular files inFoo/
, not subdirectories and other files.
– Gilles
Jan 24 '11 at 20:29
add a comment |
One way is to use find
:
find Foo/ -type f -exec mv -t Bar/ {} +
The -type f
restricts the find command to finding files. You should investigate the -type
, -maxdepth
, and -mindepth
options of find
to customize your command to account for subdirectories. Find has a lengthy but very helpful manual page.
3
That only moves the regular files inFoo/
, not subdirectories and other files.
– Gilles
Jan 24 '11 at 20:29
add a comment |
One way is to use find
:
find Foo/ -type f -exec mv -t Bar/ {} +
The -type f
restricts the find command to finding files. You should investigate the -type
, -maxdepth
, and -mindepth
options of find
to customize your command to account for subdirectories. Find has a lengthy but very helpful manual page.
One way is to use find
:
find Foo/ -type f -exec mv -t Bar/ {} +
The -type f
restricts the find command to finding files. You should investigate the -type
, -maxdepth
, and -mindepth
options of find
to customize your command to account for subdirectories. Find has a lengthy but very helpful manual page.
answered Jan 24 '11 at 19:46
Steven DSteven D
32.6k898108
32.6k898108
3
That only moves the regular files inFoo/
, not subdirectories and other files.
– Gilles
Jan 24 '11 at 20:29
add a comment |
3
That only moves the regular files inFoo/
, not subdirectories and other files.
– Gilles
Jan 24 '11 at 20:29
3
3
That only moves the regular files in
Foo/
, not subdirectories and other files.– Gilles
Jan 24 '11 at 20:29
That only moves the regular files in
Foo/
, not subdirectories and other files.– Gilles
Jan 24 '11 at 20:29
add a comment |
Try the copy command cp
:
$ cp -r myfolder/* destinationfolder
cp -r
means copy recursive, so all folders and files will be copied.
You can use the remove command rm
to remove a folder:
$ rm -r myfolder
See more: move all files from a directory to another one.
2
Not the best when you're moving a lot of large files, but I guess it would work.
– Cory Klein
May 12 '14 at 17:25
add a comment |
Try the copy command cp
:
$ cp -r myfolder/* destinationfolder
cp -r
means copy recursive, so all folders and files will be copied.
You can use the remove command rm
to remove a folder:
$ rm -r myfolder
See more: move all files from a directory to another one.
2
Not the best when you're moving a lot of large files, but I guess it would work.
– Cory Klein
May 12 '14 at 17:25
add a comment |
Try the copy command cp
:
$ cp -r myfolder/* destinationfolder
cp -r
means copy recursive, so all folders and files will be copied.
You can use the remove command rm
to remove a folder:
$ rm -r myfolder
See more: move all files from a directory to another one.
Try the copy command cp
:
$ cp -r myfolder/* destinationfolder
cp -r
means copy recursive, so all folders and files will be copied.
You can use the remove command rm
to remove a folder:
$ rm -r myfolder
See more: move all files from a directory to another one.
edited Jun 6 '15 at 15:26
kenorb
8,846372109
8,846372109
answered May 11 '14 at 2:52
ucefkhucefkh
1213
1213
2
Not the best when you're moving a lot of large files, but I guess it would work.
– Cory Klein
May 12 '14 at 17:25
add a comment |
2
Not the best when you're moving a lot of large files, but I guess it would work.
– Cory Klein
May 12 '14 at 17:25
2
2
Not the best when you're moving a lot of large files, but I guess it would work.
– Cory Klein
May 12 '14 at 17:25
Not the best when you're moving a lot of large files, but I guess it would work.
– Cory Klein
May 12 '14 at 17:25
add a comment |
Rsync is another option:
rsync -axvP --remove-source-files sourcedirectory/ targetdirectory
This works because in rsync the trailing slash matters, sourcedirectory/
refers to the content of the directory, while sourcedirectory
would refer to the directory itself.
The disadvantage of this method is that rsync will only cleanup the files after the move, not the directory. So you are left with an empty sourcedirectory tree. For workarounds for that, see:
Move files and delete directories with rsync?
So while this might not be optimal for move operations, it can be extremely useful for copy operations.
add a comment |
Rsync is another option:
rsync -axvP --remove-source-files sourcedirectory/ targetdirectory
This works because in rsync the trailing slash matters, sourcedirectory/
refers to the content of the directory, while sourcedirectory
would refer to the directory itself.
The disadvantage of this method is that rsync will only cleanup the files after the move, not the directory. So you are left with an empty sourcedirectory tree. For workarounds for that, see:
Move files and delete directories with rsync?
So while this might not be optimal for move operations, it can be extremely useful for copy operations.
add a comment |
Rsync is another option:
rsync -axvP --remove-source-files sourcedirectory/ targetdirectory
This works because in rsync the trailing slash matters, sourcedirectory/
refers to the content of the directory, while sourcedirectory
would refer to the directory itself.
The disadvantage of this method is that rsync will only cleanup the files after the move, not the directory. So you are left with an empty sourcedirectory tree. For workarounds for that, see:
Move files and delete directories with rsync?
So while this might not be optimal for move operations, it can be extremely useful for copy operations.
Rsync is another option:
rsync -axvP --remove-source-files sourcedirectory/ targetdirectory
This works because in rsync the trailing slash matters, sourcedirectory/
refers to the content of the directory, while sourcedirectory
would refer to the directory itself.
The disadvantage of this method is that rsync will only cleanup the files after the move, not the directory. So you are left with an empty sourcedirectory tree. For workarounds for that, see:
Move files and delete directories with rsync?
So while this might not be optimal for move operations, it can be extremely useful for copy operations.
answered Jul 7 '18 at 17:16
GrumbelGrumbel
1112
1112
add a comment |
add a comment |
Answer for bash/fish
Here's a way to do it using wildcards:
.[!.]* ..?*
will match all hidden files except .
and ..
.[!.]* ..?* *
will match all files (hidden or not) except .
and ..
And to answer the particular example of this question you need cd foo && mv .[!.]* ..?* * ../bar
Explanation
.[!.]*
matches file-names starting with one dot, followed by any character except the dot optionally followed by any string. This is close enough but it misses files starting with two dots like ..foo
. To include such files we add ..?*
which matches file-names starting with two dots, followed by any character, optionally followed by any string.
Test
You can test these wildcards with the commands below. I've tried them successfully under bash and fish. They fail under sh, zsh, xonsh.
mkdir temp
cd temp
touch a .b .bc ..c ..cd ...d ...de
ls .[!.]* ..?*
# you get this output:
.b .bc ..c ..cd ...d ...de
# cleanup
cd ..
rm -rf temp
add a comment |
Answer for bash/fish
Here's a way to do it using wildcards:
.[!.]* ..?*
will match all hidden files except .
and ..
.[!.]* ..?* *
will match all files (hidden or not) except .
and ..
And to answer the particular example of this question you need cd foo && mv .[!.]* ..?* * ../bar
Explanation
.[!.]*
matches file-names starting with one dot, followed by any character except the dot optionally followed by any string. This is close enough but it misses files starting with two dots like ..foo
. To include such files we add ..?*
which matches file-names starting with two dots, followed by any character, optionally followed by any string.
Test
You can test these wildcards with the commands below. I've tried them successfully under bash and fish. They fail under sh, zsh, xonsh.
mkdir temp
cd temp
touch a .b .bc ..c ..cd ...d ...de
ls .[!.]* ..?*
# you get this output:
.b .bc ..c ..cd ...d ...de
# cleanup
cd ..
rm -rf temp
add a comment |
Answer for bash/fish
Here's a way to do it using wildcards:
.[!.]* ..?*
will match all hidden files except .
and ..
.[!.]* ..?* *
will match all files (hidden or not) except .
and ..
And to answer the particular example of this question you need cd foo && mv .[!.]* ..?* * ../bar
Explanation
.[!.]*
matches file-names starting with one dot, followed by any character except the dot optionally followed by any string. This is close enough but it misses files starting with two dots like ..foo
. To include such files we add ..?*
which matches file-names starting with two dots, followed by any character, optionally followed by any string.
Test
You can test these wildcards with the commands below. I've tried them successfully under bash and fish. They fail under sh, zsh, xonsh.
mkdir temp
cd temp
touch a .b .bc ..c ..cd ...d ...de
ls .[!.]* ..?*
# you get this output:
.b .bc ..c ..cd ...d ...de
# cleanup
cd ..
rm -rf temp
Answer for bash/fish
Here's a way to do it using wildcards:
.[!.]* ..?*
will match all hidden files except .
and ..
.[!.]* ..?* *
will match all files (hidden or not) except .
and ..
And to answer the particular example of this question you need cd foo && mv .[!.]* ..?* * ../bar
Explanation
.[!.]*
matches file-names starting with one dot, followed by any character except the dot optionally followed by any string. This is close enough but it misses files starting with two dots like ..foo
. To include such files we add ..?*
which matches file-names starting with two dots, followed by any character, optionally followed by any string.
Test
You can test these wildcards with the commands below. I've tried them successfully under bash and fish. They fail under sh, zsh, xonsh.
mkdir temp
cd temp
touch a .b .bc ..c ..cd ...d ...de
ls .[!.]* ..?*
# you get this output:
.b .bc ..c ..cd ...d ...de
# cleanup
cd ..
rm -rf temp
edited Sep 11 '18 at 7:22
answered Nov 6 '17 at 15:44
ndemoundemou
554617
554617
add a comment |
add a comment |
You might also be able to find and grep with backquotes to select files for the move command. Pass those into mv.
I.e. For hidden files
find Foo -maxdepth 1 | egrep '^Foo/[.]' # Output: .hidden
So
mv `find Foo -maxdepth 1 | egrep '^Foo/[.]'` Bar # mv Foo/.hidden Bar
Moves only selected hidden files into Bar
:
mv `find Foo -maxdepth 1 | egrep '^Foo/.'` Bar # mv Foo/.hidden Foo/notHidden Bar
Moves all files in Foo to Bar since the '.' in the egrep command acts as a wildcard without the square brackets.
The ^
character ensures the match starts from the beginning of the line.
Some details of egrep
pattern matching can be found here.
Using maxdepth 1
stops find from going into subdirectories.
add a comment |
You might also be able to find and grep with backquotes to select files for the move command. Pass those into mv.
I.e. For hidden files
find Foo -maxdepth 1 | egrep '^Foo/[.]' # Output: .hidden
So
mv `find Foo -maxdepth 1 | egrep '^Foo/[.]'` Bar # mv Foo/.hidden Bar
Moves only selected hidden files into Bar
:
mv `find Foo -maxdepth 1 | egrep '^Foo/.'` Bar # mv Foo/.hidden Foo/notHidden Bar
Moves all files in Foo to Bar since the '.' in the egrep command acts as a wildcard without the square brackets.
The ^
character ensures the match starts from the beginning of the line.
Some details of egrep
pattern matching can be found here.
Using maxdepth 1
stops find from going into subdirectories.
add a comment |
You might also be able to find and grep with backquotes to select files for the move command. Pass those into mv.
I.e. For hidden files
find Foo -maxdepth 1 | egrep '^Foo/[.]' # Output: .hidden
So
mv `find Foo -maxdepth 1 | egrep '^Foo/[.]'` Bar # mv Foo/.hidden Bar
Moves only selected hidden files into Bar
:
mv `find Foo -maxdepth 1 | egrep '^Foo/.'` Bar # mv Foo/.hidden Foo/notHidden Bar
Moves all files in Foo to Bar since the '.' in the egrep command acts as a wildcard without the square brackets.
The ^
character ensures the match starts from the beginning of the line.
Some details of egrep
pattern matching can be found here.
Using maxdepth 1
stops find from going into subdirectories.
You might also be able to find and grep with backquotes to select files for the move command. Pass those into mv.
I.e. For hidden files
find Foo -maxdepth 1 | egrep '^Foo/[.]' # Output: .hidden
So
mv `find Foo -maxdepth 1 | egrep '^Foo/[.]'` Bar # mv Foo/.hidden Bar
Moves only selected hidden files into Bar
:
mv `find Foo -maxdepth 1 | egrep '^Foo/.'` Bar # mv Foo/.hidden Foo/notHidden Bar
Moves all files in Foo to Bar since the '.' in the egrep command acts as a wildcard without the square brackets.
The ^
character ensures the match starts from the beginning of the line.
Some details of egrep
pattern matching can be found here.
Using maxdepth 1
stops find from going into subdirectories.
edited Jun 6 '15 at 15:26
kenorb
8,846372109
8,846372109
answered May 12 '14 at 22:30
user3192145user3192145
11
11
add a comment |
add a comment |
Inspired from this answer:
Without copying the files...
rsync -ax --link-dest=../Foo/ Foo/ Bar
Caveats:
--link-dest
path must be absolute or relative to DESTINATION (Bar
in the example)You've to put
/
after SOURCE (Foo/
in the example), otherwise it
will copy the SOURCE folder instead of contents of it.
add a comment |
Inspired from this answer:
Without copying the files...
rsync -ax --link-dest=../Foo/ Foo/ Bar
Caveats:
--link-dest
path must be absolute or relative to DESTINATION (Bar
in the example)You've to put
/
after SOURCE (Foo/
in the example), otherwise it
will copy the SOURCE folder instead of contents of it.
add a comment |
Inspired from this answer:
Without copying the files...
rsync -ax --link-dest=../Foo/ Foo/ Bar
Caveats:
--link-dest
path must be absolute or relative to DESTINATION (Bar
in the example)You've to put
/
after SOURCE (Foo/
in the example), otherwise it
will copy the SOURCE folder instead of contents of it.
Inspired from this answer:
Without copying the files...
rsync -ax --link-dest=../Foo/ Foo/ Bar
Caveats:
--link-dest
path must be absolute or relative to DESTINATION (Bar
in the example)You've to put
/
after SOURCE (Foo/
in the example), otherwise it
will copy the SOURCE folder instead of contents of it.
edited Apr 13 '17 at 12:13
Community♦
1
1
answered Dec 23 '15 at 8:40
palindrompalindrom
1011
1011
add a comment |
add a comment |
I find that this works well for bash and there's no need to change shell options
mv sourcedir/{*,.[^.]*} destdir/
New contributor
This basically duplicates ndemou’s answer except you added curly braces (without explaining them). But (1) your answer doesn’t match files whose names begin with..
, and (2) to be POSIX compliant, you should use!
instead of^
; i.e.,{*,.[!.]*}
.
– G-Man
9 mins ago
add a comment |
I find that this works well for bash and there's no need to change shell options
mv sourcedir/{*,.[^.]*} destdir/
New contributor
This basically duplicates ndemou’s answer except you added curly braces (without explaining them). But (1) your answer doesn’t match files whose names begin with..
, and (2) to be POSIX compliant, you should use!
instead of^
; i.e.,{*,.[!.]*}
.
– G-Man
9 mins ago
add a comment |
I find that this works well for bash and there's no need to change shell options
mv sourcedir/{*,.[^.]*} destdir/
New contributor
I find that this works well for bash and there's no need to change shell options
mv sourcedir/{*,.[^.]*} destdir/
New contributor
New contributor
answered 29 mins ago
cmndr sp0ckcmndr sp0ck
1
1
New contributor
New contributor
This basically duplicates ndemou’s answer except you added curly braces (without explaining them). But (1) your answer doesn’t match files whose names begin with..
, and (2) to be POSIX compliant, you should use!
instead of^
; i.e.,{*,.[!.]*}
.
– G-Man
9 mins ago
add a comment |
This basically duplicates ndemou’s answer except you added curly braces (without explaining them). But (1) your answer doesn’t match files whose names begin with..
, and (2) to be POSIX compliant, you should use!
instead of^
; i.e.,{*,.[!.]*}
.
– G-Man
9 mins ago
This basically duplicates ndemou’s answer except you added curly braces (without explaining them). But (1) your answer doesn’t match files whose names begin with
..
, and (2) to be POSIX compliant, you should use !
instead of ^
; i.e., {*,.[!.]*}
.– G-Man
9 mins ago
This basically duplicates ndemou’s answer except you added curly braces (without explaining them). But (1) your answer doesn’t match files whose names begin with
..
, and (2) to be POSIX compliant, you should use !
instead of ^
; i.e., {*,.[!.]*}
.– G-Man
9 mins ago
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f6393%2fhow-do-you-move-all-files-including-hidden-from-one-directory-to-another%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Linux: How to move all files from current directory to upper directory ?
– Andrew T Finnell
Jan 24 '11 at 19:36
Bah, I knew I should have refreshed the answers before typing mine up. Very helpful link.
– Steven D
Jan 24 '11 at 19:48
Sadly, this question ended up here because I said on SO that it should move here or SU, so of course it moved here when there was a duplicate on SU already :)
– Michael Mrozek♦
Jan 24 '11 at 20:06
Personally, I think *nix specific questions and answers should be off-topic on SU. With the current rules we end up with tons of content collisions between the two - like this question.
– Cory Klein
May 12 '14 at 17:27