28

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?

2 Answers 2

36
$ cd /path/to/folderA
$ mv folderB/* .
$ rmdir folderB
4
  • 1
    mv folderB/* . ? what is the dot? Commented Jun 11, 2010 at 16:44
  • 12
    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
    Commented Jun 11, 2010 at 17:36
  • 1
    This fails if folderB/folderB exists, so beware of using it in scripts.
    – filipos
    Commented Feb 19, 2016 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 Commented Apr 5, 2016 at 15:10
2

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"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .