[ How to move all files including hidden files into parent directory via * ]
Its must be a popular question but I could not find an answer.
How to move all files via * including hidden files as well to parent directory like this:
mv /path/subfolder/* /path/
This will move all files to parent directory like expected but will not move hidden files. How to do that?
Answer 1
You can find a comprehensive set of solutions on this in UNIX & Linux's answer to How do you move all files (including hidden) from one directory to another?. It shows solutions in Bash, zsh, ksh93, standard (POSIX) sh, etc.
You can use these two commands together:
mv /path/subfolder/* /path/ # your current approach
mv /path/subfolder/.* /path/ # this one for hidden files
Or all together (thanks pfnuesel):
mv /path/subfolder/{.,}* /path/
Which expands to:
mv /path/subfolder/* /path/subfolder/.* /path/
(example: echo a{.,}b
expands to a.b ab
)
Note this will show a couple of warnings:
mv: cannot move ‘/path/subfolder/.’ to /path/.’: Device or resource busy
mv: cannot remove /path/subfolder/..’: Is a directory
Just ignore them: this happens because /path/subfolder/{.,}*
also expands to /path/subfolder/.
and /path/subfolder/..
, which are the directory and the parent directory (See What do “.” and “..” mean when in a folder?).
If you want to just copy, you can use a mere:
cp -r /path/subfolder/. /path/
# ^
# note the dot!
This will copy all files, both normal and hidden ones, since /path/subfolder/.
expands to "everything from this directory" (Source: How to copy with cp to include hidden files and hidden directories and their contents?)
Answer 2
This will move all files to parent directory like expected but will not move hidden files. How to do that?
You could turn on dotglob
:
shopt -s dotglob # This would cause mv below to match hidden files
mv /path/subfolder/* /path/
In order to turn off dotglob
, you'd need to say:
shopt -u dotglob
Answer 3
I think this is the most elegant, as it also does not try to move '..':
mv /source/path/{.[!.],}* /destination/path
Answer 4
Let me introduce you to my friend "dotglob". It turns on and off whether or not "*" includes hidden files.
$ mkdir test
$ cd test
$ touch a b c .hidden .hi .den
$ ls -a
. .. .den .hi .hidden a b c
$ shopt -u dotglob
$ ls *
a b c
$ for i in * ; do echo I found: $i ; done
I found: a
I found: b
I found: c
$ shopt -s dotglob
$ ls *
.den .hi .hidden a b c
$ for i in * ; do echo I found: $i ; done
I found: .den
I found: .hi
I found: .hidden
I found: a
I found: b
I found: c
It defaults to "off".
$ shopt dotglob
dotglob off
It is best to turn it back on when you are done otherwise you will confuse things that assume it will be off.
Answer 5
Alternative simpler solution is to use rsync
utility:
rsync -vuar --delete-after path/subfolder/ path/
The advantage is that the original folder (subfolder
) would be removed as well as part of the command, and when using mv
examples here you still need to clean up your folders, not to mention additional headache to cover hidden and non-hidden files in one single pattern.
In addition rsync
provides support of copying/moving files between remotes and it would make sure that files are copied exactly as they originally were (-a
).
The used -u
parameter would skip existing newer files, -r
recurse into directories and -v
would increase verbosity.
Answer 6
By using the find
command in conjunction with the mv
command, you can prevent the mv
command from trying to move directories (e.g. ..
and .
) and subdirectories. Here's one option:
find /path/subfolder -maxdepth 1 -type f -name '*' -exec mv -n {} /path \;
There are problems with some of the other answers provided. For example, each of the following will try to move subdirectories from the source path:
1) mv /path/subfolder/* /path/ ; mv /path/subfolder/.* /path/
2) mv /path/subfolder/{.,}* /path/
3) mv /source/path/{.[!.],}* /destination/path
Also, 2) includes the . and .. files and 3) misses files like ..foobar, ...barfoo, etc.
You could use, mv /source/path/{.[!.],..?,}* /destination/path
, which would include the files missed by 3), but it would still try to move subdirectories. Using the find
command with the mv
command as I describe above eliminates all these problems.