How to recursively move all files (including hidden) in a subfolder into a parent folder in *nix?
Solution 1
In Bash (and some others), you can use brace expansion to accomplish this in one line:
mv bar/{,.}* .
The comma separates a null and a dot, so the mv
command sees filenames that match *
and .*
Solution 2
This one harvests all files from subfolders and moves them to current dir
find . -type f -exec mv -iv \{} . \;
If You want to owerwrite files with same name, use
yes y | find . -type f -exec mv -iv \{} . \;
Solution 3
First thing to know about globbing --it's done by the shell, not the command. Check your shell's man page for all the details.
Solution 4
The easiest way to do this is to do it in two command, because * doesn't match .whatever
cd /foo
mv bar/* ./
mv bar/.??* ./
You do not want to use bar/.* which I found out while committing this mistake:
rm -rf ./.*
This is a BAD THING. Anyone want to guess why? ;-)
Solution 5
mv .??* *
will take care of anything except dot followed by a single character. If that's common for your situation, you can add .[a-zA-Z0-9]*
. That will still leave files with names such as .;
, .^
, and .^I
(tab). If you need to handle everything, you'll need to be a bit more complex.
mv .. `ls -f | egrep -v '^.$|^..$'
Related videos on Youtube
deadprogrammer
Updated on September 18, 2022Comments
-
deadprogrammer almost 2 years
This is a bit of an embarrassing question, but I have to admit that this late in my career I still have questions about the mv command.
I frequently have this problem: I need to move all files recursively up one level. Let's say I have folder foo, and a folder bar inside it. Bar has a mess of files and folders, including dot files and folders. How do I move everything in bar to the foo level?
If foo is empty, I simply move bar one level above, delete foo and rename bar into foo. Part of the problem is that I can't figure out what mv's wildcard for "everything including dots" is. A part of this question is this - is there an in-depth discussion of the wildcards that cp and mv commands use somewhere (googling this only brings very basic tutorials).
-
Mikael S over 14 yearsI don't think Bash or Zsh expands
.*
to.
and..
. Zsh doesn't for me at least. -
Matt Simmons over 14 yearsMikael: I can promise that bash does (or at least did), as I had to recover user directories that I wiped out by doing just that
-
Xananax about 12 yearsthis attempts to move '..' too, fails with message 'resource is busy'. Works nonetheless.
-
HopelessN00b over 11 yearsSuggested by anonymous user:
To eliminate the error caused by also matching "." and "..", use this command: mv bar/{,.[!.],..?}* .