# All I want to do is move a directory :(

**URL:** <https://rubytalk.org/t/all-i-want-to-do-is-move-a-directory/15439>\
**Category:** ruby-talk\
**Created:** [22 December 2004 18:35 UTC](https://rubytalk.org/t/all-i-want-to-do-is-move-a-directory/15439 "2004-12-22T18:35:50Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)\
**Post date:** [22 December 2004 18:35 UTC](https://rubytalk.org/t/all-i-want-to-do-is-move-a-directory/15439/1 "2004-12-22T18:35:50Z")

</div>

Very frustrated. I have just spent well over an hour trying to do the simplest  
of all things: move a directory. Of course, I want to do it in Ruby but this  
doesn't work:

&nbsp;&nbsp;[FileUtils.mv](http://FileUtils.mv)( @dir, ".trash/" )

Could someone please show me why am I apparently so stupid.

Thanks,  
T.

---

<div class="post-metadata">

**Author:** ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)\
**Post date:** [22 December 2004 19:38 UTC](https://rubytalk.org/t/all-i-want-to-do-is-move-a-directory/15439/2 "2004-12-22T19:38:07Z")

</div>

Thanks. That almost works. But the oddest thing happens. If @dir already  
exists, it moves it to @dir/@dir. If @dir/@dir already exists it bombs. I  
can't figure out why and am starting to think its a bug with #mv.

T.

> **···**
>
> On Wednesday 22 December 2004 02:14 pm, you wrote:
> 
> > -----BEGIN PGP SIGNED MESSAGE-----  
> > Hash: SHA1
> > 
> > trans. (T. Onoma) wrote:  
> > \> FileUtils.mv( @dir, ".trash/" )  
> > \>  
> > \> Could someone please show me why am I apparently so stupid.
> > 
> > I don't think it's smart enough to make subdir for you. Try:  
> > &nbsp;&nbsp;FileUtils.mv(@dir, File.join('.trash', File.basename(@dir)))

---

<div class="post-metadata">

**Author:** ![Gennady1](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@Gennady1](https://rubytalk.org/u/Gennady1)\
**Post date:** [22 December 2004 21:25 UTC](https://rubytalk.org/t/all-i-want-to-do-is-move-a-directory/15439/3 "2004-12-22T21:25:49Z")

</div>

trans. (T. Onoma) wrote:

> Very frustrated. I have just spent well over an hour trying to do the simplest of all things: move a directory. Of course, I want to do it in Ruby but this doesn't work:
> 
> &nbsp;&nbsp;FileUtils.mv( @dir, ".trash/" )
> 
> Could someone please show me why am I apparently so stupid.
> 
> Thanks,  
> T.

What excactly are you experiencing? On what platform? On my Linux box with Ruby 1.6.8 when I do

$ mkdir aaa  
$ mkdir .trash

$ irb -r fileutils  
irb(main):001:0\> FileUtils.mv "aaa", ".trash/"

it works just fine. The only thing is that if ".trash/aaa" already exists, I get an exception:

Errno::EISDIR: Is a directory - ".trash/aaa"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/site\_ruby/1.6/fileutils.rb:396:in `open'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/site\_ruby/1.6/fileutils.rb:396:in `copy\_file'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/site\_ruby/1.6/fileutils.rb:395:in `open'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/site\_ruby/1.6/fileutils.rb:395:in `copy\_file'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/site\_ruby/1.6/fileutils.rb:445:in `mv'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/site\_ruby/1.6/fileutils.rb:432:in `fu\_each\_src\_dest'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/local/lib/ruby/site\_ruby/1.6/fileutils.rb:432:in `mv'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1

Gennady.

---

<div class="post-metadata">

**Author:** ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)\
**Post date:** [22 December 2004 23:22 UTC](https://rubytalk.org/t/all-i-want-to-do-is-move-a-directory/15439/4 "2004-12-22T23:22:54Z")

</div>

Yes. That is one of the problems.

T.

> **···**
>
> On Wednesday 22 December 2004 04:25 pm, Gennady Bystritksy wrote:
> 
> > trans. (T. Onoma) wrote:  
> > \> Very frustrated. I have just spent well over an hour trying to do the  
> > \> simplest of all things: move a directory. Of course, I want to do it in  
> > \> Ruby but this doesn't work:  
> > \>  
> > \> FileUtils.mv( @dir, ".trash/" )  
> > \>  
> > \> Could someone please show me why am I apparently so stupid.  
> > \>  
> > \> Thanks,  
> > \> T.
> > 
> > What excactly are you experiencing? On what platform? On my Linux box  
> > with Ruby 1.6.8 when I do
> > 
> > $ mkdir aaa  
> > $ mkdir .trash
> > 
> > $ irb -r fileutils  
> > irb(main):001:0\> FileUtils.mv "aaa", ".trash/"

---

<div class="post-metadata">

**Author:** ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)\
**Post date:** [23 December 2004 00:05 UTC](https://rubytalk.org/t/all-i-want-to-do-is-move-a-directory/15439/5 "2004-12-23T00:05:37Z")

</div>

I think the problem may be that the :force option isn't working correctly on  
FilUtils#mv. Isn't that supposed to force the move even if the file/directory  
is already there?

T.

---

<div class="post-metadata">

**Author:** ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)\
**Post date:** [23 December 2004 00:30 UTC](https://rubytalk.org/t/all-i-want-to-do-is-move-a-directory/15439/6 "2004-12-23T00:30:41Z")

</div>

FYI: ruby 1.8.2p2 on Debian Testing

> **···**
>
> On Wednesday 22 December 2004 07:05 pm, trans. (T. Onoma) wrote:
> 
> > I think the problem may be that the :force option isn't working correctly  
> > on FilUtils#mv. Isn't that supposed to force the move even if the  
> > file/directory is already there?

---

<div class="post-metadata">

**Author:** ![Minero\_Aoki](https://avatars.discourse-cdn.com/v4/letter/m/71c47a/32.png) [@Minero\_Aoki](https://rubytalk.org/u/Minero_Aoki)\
**Post date:** [23 December 2004 01:04 UTC](https://rubytalk.org/t/all-i-want-to-do-is-move-a-directory/15439/7 "2004-12-23T01:04:49Z")

</div>

Hi,

&nbsp;&nbsp;In mail "Re: All I want to do is move a directory :("

> I think the problem may be that the :force option isn't working correctly on  
> FilUtils#mv. Isn't that supposed to force the move even if the file/directory  
> is already there?

It depends on what is the \*correct\* behavior.  
At least GNU mv does not overwrite directory:

&nbsp;&nbsp;% find  
&nbsp;&nbsp;.  
&nbsp;&nbsp;./trash  
&nbsp;&nbsp;./trash/a  
&nbsp;&nbsp;./trash/a/test  
&nbsp;&nbsp;./a  
&nbsp;&nbsp;./a/test  
&nbsp;&nbsp;./a/test2  
&nbsp;&nbsp;% mv a trash  
&nbsp;&nbsp;mv: cannot overwrite directory `trash/a'  
&nbsp;&nbsp;% mv -f a trash  
&nbsp;&nbsp;mv: cannot overwrite directory `trash/a'  
&nbsp;&nbsp;% find  
&nbsp;&nbsp;.  
&nbsp;&nbsp;./trash  
&nbsp;&nbsp;./trash/a  
&nbsp;&nbsp;./trash/a/test  
&nbsp;&nbsp;./a  
&nbsp;&nbsp;./a/test  
&nbsp;&nbsp;./a/test2  
&nbsp;&nbsp;%

In my opinion, :force option ensures only that #mv does not  
raise exception, it does not imply continuing process.

Regards,  
Minero Aoki

> **···**
>
> "trans. (T. Onoma)" \<transami@runbox.com\> wrote:

---

<div class="post-metadata">

**Author:** ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)\
**Post date:** [23 December 2004 04:12 UTC](https://rubytalk.org/t/all-i-want-to-do-is-move-a-directory/15439/8 "2004-12-23T04:12:17Z")

</div>

Hmm. I thought for sure -f overwrote, but indeed you are right. So that means  
one must perform a 'rm -r' first? If so that's really bad, as it is a much  
more dangerous way to have to go about it. If the wrong directory name got in  
there it could spell the end of one's machine ☹ Is there no way to simply  
(and truly) \_force\_ a move?

Thanks,  
T.

> **···**
>
> On Wednesday 22 December 2004 08:04 pm, Minero Aoki wrote:
> 
> > It depends on what is the \*correct\* behavior.  
> > At least GNU mv does not overwrite directory:
> > 
> > &nbsp;&nbsp;% find  
> > &nbsp;&nbsp;.  
> > &nbsp;&nbsp;./trash  
> > &nbsp;&nbsp;./trash/a  
> > &nbsp;&nbsp;./trash/a/test  
> > &nbsp;&nbsp;./a  
> > &nbsp;&nbsp;./a/test  
> > &nbsp;&nbsp;./a/test2  
> > &nbsp;&nbsp;% mv a trash  
> > &nbsp;&nbsp;mv: cannot overwrite directory `trash/a'  
> > &nbsp;&nbsp;% mv -f a trash  
> > &nbsp;&nbsp;mv: cannot overwrite directory `trash/a'  
> > &nbsp;&nbsp;% find  
> > &nbsp;&nbsp;.  
> > &nbsp;&nbsp;./trash  
> > &nbsp;&nbsp;./trash/a  
> > &nbsp;&nbsp;./trash/a/test  
> > &nbsp;&nbsp;./a  
> > &nbsp;&nbsp;./a/test  
> > &nbsp;&nbsp;./a/test2  
> > &nbsp;&nbsp;%
> > 
> > In my opinion, :force option ensures only that #mv does not  
> > raise exception, it does not imply continuing process.

---

<div class="post-metadata">

**Author:** ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)\
**Post date:** [23 December 2004 14:54 UTC](https://rubytalk.org/t/all-i-want-to-do-is-move-a-directory/15439/9 "2004-12-23T14:54:03Z")

</div>

Well, that doesn;t sound like "forcing" to me, but ...

Sigh, now I'm getting opposite behavior. I tried to create irb session to  
demonstrate the bug I was getting (@dir/@dir) but instead now it seems to be  
overwriting:

irb(main):001:0\> require 'fileutils'  
=\> true  
irb(main):002:0\> puts Dir['\*\*/\*']  
a  
test  
a/test.txt  
test/a  
=\> nil  
irb(main):003:0\> FileUtils.mv( "a", "test" )  
=\> 0  
irb(main):004:0\> puts Dir['\*\*/\*']  
test  
test/a  
test/a/test.txt

I traced it to the fact the "a" is empty. If one tries this when "a" is not  
empty it then fails. This doesn't seem to be "UNIX correct" either.

T.

> **···**
>
> On Wednesday 22 December 2004 08:04 pm, Minero Aoki wrote:
> 
> > In my opinion, :force option ensures only that #mv does not  
> > raise exception, it does not imply continuing process.

---

<div class="post-metadata">

**Author:** ![BIl\_Kleb1](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@BIl\_Kleb1](https://rubytalk.org/u/BIl_Kleb1)\
**Post date:** [25 December 2004 20:56 UTC](https://rubytalk.org/t/all-i-want-to-do-is-move-a-directory/15439/10 "2004-12-25T20:56:59Z")

</div>

Minero Aoki wrote:

> In my opinion, :force option ensures only that #mv does not  
> raise exception, it does not imply continuing process.

That sounds counter to the -f option for Un\*x's 'mv' command.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-f, --force do not prompt before overwriting

Why change the convention to which so many are accustomed?

Regards,

> **···**
>
> --  
> Bil Kleb, Hampton, Virginia  
> [http://fun3d.larc.nasa.gov](http://fun3d.larc.nasa.gov)

---

<div class="post-metadata">

**Author:** ![Mark\_Hubbart1](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Mark\_Hubbart1](https://rubytalk.org/u/Mark_Hubbart1)\
**Post date:** [23 December 2004 07:54 UTC](https://rubytalk.org/t/all-i-want-to-do-is-move-a-directory/15439/11 "2004-12-23T07:54:19Z")

</div>

It's time to roll your own. If you don't have to worry about moving  
across filesystems, a simple File.rename will work:

irb(main):001:0\> Dir.mkdir ".trash"  
=\> 0  
irb(main):002:0\> Dir.mkdir "aaa"  
=\> 0  
irb(main):003:0\> Dir.mkdir ".trash/aaa"  
=\> 0  
irb(main):004:0\> Dir.mkdir "aaa/foo"  
=\> 0  
irb(main):005:0\> File.rename "./aaa", "./.trash/aaa"  
=\> 0  
irb(main):012:0\> puts Dir['.trash/\*\*/\*']  
.trash/aaa  
.trash/aaa/foo

If you attempt a move across filesystems, rename should raise an  
error. You could catch that error and change strategies to a  
copy/delete sequence. That would be messier, but...

cheers,  
Mark

> **···**
>
> On Thu, 23 Dec 2004 13:12:17 +0900, trans. (T. Onoma) \<transami@runbox.com\> wrote:
> 
> > Hmm. I thought for sure -f overwrote, but indeed you are right. So that means  
> > one must perform a 'rm -r' first? If so that's really bad, as it is a much  
> > more dangerous way to have to go about it. If the wrong directory name got in  
> > there it could spell the end of one's machine ☹ Is there no way to simply  
> > (and truly) \_force\_ a move?

---

<div class="post-metadata">

**Author:** ![Ilmari\_Heikkinen1](https://avatars.discourse-cdn.com/v4/letter/i/fbc32d/32.png) [@Ilmari\_Heikkinen1](https://rubytalk.org/u/Ilmari_Heikkinen1)\
**Post date:** [23 December 2004 12:50 UTC](https://rubytalk.org/t/all-i-want-to-do-is-move-a-directory/15439/12 "2004-12-23T12:50:32Z")

</div>

How would a forced move differ from rm -rf followed by move?

-Ilmari

> **···**
>
> On 23.12.2004, at 06:12, trans. (T. Onoma) wrote:
> 
> > If so that's really bad, as it is a much  
> > more dangerous way to have to go about it. If the wrong directory name got in  
> > there it could spell the end of one's machine ☹ Is there no way to simply  
> > (and truly) \_force\_ a move?

---

<div class="post-metadata">

**Author:** ![Gennady1](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@Gennady1](https://rubytalk.org/u/Gennady1)\
**Post date:** [25 December 2004 21:10 UTC](https://rubytalk.org/t/all-i-want-to-do-is-move-a-directory/15439/13 "2004-12-25T21:10:57Z")

</div>

'mv' command does not override existing directories if they are not empty even with -f option. I cannot try it right now on Linux, but in Darwin it even prints out the error message, despite -f option.

Gennady.

> **···**
>
> On Dec 25, 2004, at 12:56 PM, Bil Kleb wrote:
> 
> > Minero Aoki wrote:
> > 
> > > In my opinion, :force option ensures only that #mv does not  
> > > raise exception, it does not imply continuing process.
> > 
> > That sounds counter to the -f option for Un\*x's 'mv' command.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-f, --force do not prompt before overwriting
> > 
> > Why change the convention to which so many are accustomed?
> > 
> > Regards,  
> > --  
> > Bil Kleb, Hampton, Virginia  
> > [http://fun3d.larc.nasa.gov](http://fun3d.larc.nasa.gov)
> 
> Sincerely,  
> Gennady Bystritsky

---

<div class="post-metadata">

**Author:** ![Lionel\_Thiry2](https://avatars.discourse-cdn.com/v4/letter/l/74df32/32.png) [@Lionel\_Thiry2](https://rubytalk.org/u/Lionel_Thiry2)\
**Post date:** [25 December 2004 22:02 UTC](https://rubytalk.org/t/all-i-want-to-do-is-move-a-directory/15439/14 "2004-12-25T22:02:10Z")

</div>

> irb(main):001:0\> require 'fileutils'  
> =\> true  
> irb(main):002:0\> puts Dir['\*\*/\*']  
> a  
> test  
> a/test.txt  
> test/a  
> =\> nil  
> irb(main):003:0\> FileUtils.mv( "a", "test" )  
> =\> 0

Maybe FileUtils.mv( "a", "test" ) and FileUtils( "a", "test/" ) doesn't do the same thing?

Lionel Thiry

---

<div class="post-metadata">

**Author:** ![Minero\_Aoki](https://avatars.discourse-cdn.com/v4/letter/m/71c47a/32.png) [@Minero\_Aoki](https://rubytalk.org/u/Minero_Aoki)\
**Post date:** [27 December 2004 07:16 UTC](https://rubytalk.org/t/all-i-want-to-do-is-move-a-directory/15439/15 "2004-12-27T07:16:14Z")

</div>

Hi,

&nbsp;&nbsp;In mail "Re: All I want to do is move a directory :("

> **···**
>
> "trans. (T. Onoma)" \<transami@runbox.com\> wrote:
> 
> > Sigh, now I'm getting opposite behavior. I tried to create irb session to  
> > demonstrate the bug I was getting (@dir/@dir) but instead now it seems to be  
> > overwriting:
> > 
> > irb(main):001:0\> require 'fileutils'  
> > =\> true  
> > irb(main):002:0\> puts Dir['\*\*/\*']  
> > a  
> > test  
> > a/test.txt  
> > test/a  
> > =\> nil  
> > irb(main):003:0\> FileUtils.mv( "a", "test" )  
> > =\> 0  
> > irb(main):004:0\> puts Dir['\*\*/\*']  
> > test  
> > test/a  
> > test/a/test.txt
> > 
> > I traced it to the fact the "a" is empty. If one tries this when "a" is not  
> > empty it then fails. This doesn't seem to be "UNIX correct" either.
> 
> This is a bug... fixed now.  
> Thank you!
> 
> Regards,  
> Minero Aoki

---

<div class="post-metadata">

**Author:** ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)\
**Post date:** [23 December 2004 14:41 UTC](https://rubytalk.org/t/all-i-want-to-do-is-move-a-directory/15439/16 "2004-12-23T14:41:14Z")

</div>

Hi --

When you move something (file or directory) to a directory it goes \_into\_ that  
directory rather then on top of it.

T.

> **···**
>
> On Thursday 23 December 2004 07:50 am, Ilmari Heikkinen wrote:
> 
> > On 23.12.2004, at 06:12, trans. (T. Onoma) wrote:  
> > \> If so that's really bad, as it is a much  
> > \> more dangerous way to have to go about it. If the wrong directory name  
> > \> got in  
> > \> there it could spell the end of one's machine ☹ Is there no way to  
> > \> simply  
> > \> (and truly) \_force\_ a move?
> > 
> > How would a forced move differ from rm -rf followed by move?
