I have a folderA that contains folderB that contains a lot of files. I would like to get rid of folderB, but not its contents. I want those contents to be inside of folderA. How can I accomplish this on the commandline?

$ cd /path/to/folderA
$ mv folderB/* .
$ rmdir folderB
  • 1
    mv folderB/* . ? what is the dot? – NewLinuxUser Jun 11 '10 at 16:44
  • 8
    Watch out for dot files (files whose name begins with .) as this will not include those. Do mv folderB/.* . to move them as well. @NewLinuxUser, the dot in your question is an alias for the working directory (in this case, folderA). – Brian Jun 11 '10 at 17:36
  • 1
    This fails if folderB/folderB exists, so beware of using it in scripts. – filipos Feb 19 '16 at 15:21
  • 1
    This also fails if folderB contains an insane amount of files. You will see bash: /bin/mv: Argument list too long because of the use of *. If that's the case use mv in combination with find as stated by @amphetamachine, or with a for loop – Javier de la Rosa Apr 5 '16 at 15:10

Quick answer:

cd /path/to/folderA
find folderB -maxdepth 1 -mindepth 1 -exec mv {} . \;
rmdir folderB

Code-hardy answer:

cd /path/to/folderA
folderB_temp="$(mktemp -d -t folderB.XXXXXX)"
mv folderB "$folderB_temp"
find "$folderB_temp/folderB" -maxdepth 1 -mindepth 1 -exec mv {} . \;
rmdir --parents --ignore-fail-on-non-empty "$folderB_temp/folderB"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.