Replace existing folder with mv command

35,545

Solution 1

If ~/oldstuff does not exist,

mv ~/newstuff ~/oldstuff

will rename newstuff to oldstuff. If it exists, it will move newstuff into oldstuff.

So, to answer your question, first remove ~/oldstuff (or rename it to olderstuff, see this question again on how to do it), then use the mv command as you did.

Solution 2

Another solution would be to use rsync. (Be careful with the trailing slashes. They are important).

This will copy everything in newstuff into oldstuff.

rsync -av ~/newstuff/ ~/oldstuff

And, the code below will copy everything in newstuff into oldstuff, and delete anything in oldstuff which is not in newstuff.

rsync -av --delete ~/newstuff/ ~/oldstuff

Note that neither of these commands will do anything to the files in ~/newstuff though. If you want to delete them, you'll have to do the rm command separately.

Solution 3

You can try:

mv -f folder1/* folder2 && rmdir folder1

Will move everything in folder1, including files and directories to folder2.

-f: do not prompt before overwriting equivalent to --reply=yes.

mv man page.

Solution 4

You have at least two options.

mv ~/newstuff/* ~/oldstuff
rmdir ~/newstuff
mv ~/oldstuff ~/newstuff

and

mv ~/oldstuff ~/ancientstuff
mv ~/newstuff ~/oldstuff
Share:
35,545

Related videos on Youtube

Jacob Wood
Author by

Jacob Wood

Updated on September 18, 2022

Comments

  • Jacob Wood
    Jacob Wood over 1 year

    Apologies if this question has already been asked, I couldn't find anything…

    Lets say I want to replace 'oldstuff' with 'newstuff'. Basically re-naming to replace. I try using this command:

    mv ~/newstuff ~/oldstuff
    

    But that only moves the folder 'newstuff' into the 'oldstuff' folder.

    How would I replace 'oldstuff' with 'newstuff'?

    I am running OS X 10.7.

  • Jacob Wood
    Jacob Wood almost 11 years
    oldstuff does exist though. That's the issue, is there anyway to do it without issuing a delete command first?
  • choroba
    choroba almost 11 years
    @JacobLukeWood: There cannot be two directories with the same name. If you want to move the contents into an existing directory, use mv newstuff/* oldstuff (could get more complicated if .files are involved).
  • Jacob Wood
    Jacob Wood almost 11 years
    I ended up doing what you said using the wildcard (*) with it, thanks :)
  • Leo Galleguillos
    Leo Galleguillos over 4 years
    This should be the accepted answer.