Search code examples
unixmvrm

Replace one directory with another


In unix, if I wanted to replace one file (e.g., foo) with another (e.g., bar), I would call

$ mv bar foo

foo would disappear, and bar would take its name.

Yet, if I wanted to replace one directory with another (again, foo and bar), this would not work:

$ mv bar foo

Here, the bar directory would be moved inside of the foo directory.

To replace bar with foo, I know I can do

$ rm -r foo
$ mv bar foo

But is there a way to accomplish this with one command?


Solution

  • It's how mv works, when destination dir isn't exists - it will do move/rename, when exists - will move source dir into it. If you really need to do this often and would like to simplify this, you can use alias, for example smth like this:

    rmandmv() {
        rm -r $2
        mv $1 $2
    }
    alias rmmv=rmandmv