All I want to do is move a directory :(

T

trans. (T. Onoma)

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.
 
T

trans. (T. Onoma)

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:
| FileUtils.mv(@dir, File.join('.trash', File.basename(@dir)))

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.
 
G

Gennady Bystritksy

trans. (T. Onoma) said:
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/"

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"
from /usr/local/lib/ruby/site_ruby/1.6/fileutils.rb:396:in `open'
from /usr/local/lib/ruby/site_ruby/1.6/fileutils.rb:396:in
`copy_file'
from /usr/local/lib/ruby/site_ruby/1.6/fileutils.rb:395:in `open'
from /usr/local/lib/ruby/site_ruby/1.6/fileutils.rb:395:in
`copy_file'
from /usr/local/lib/ruby/site_ruby/1.6/fileutils.rb:445:in `mv'
from /usr/local/lib/ruby/site_ruby/1.6/fileutils.rb:432:in
`fu_each_src_dest'
from /usr/local/lib/ruby/site_ruby/1.6/fileutils.rb:432:in `mv'
from (irb):1


Gennady.
 
T

trans. (T. Onoma)

| > 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/"

Yes. That is one of the problems.

T.
 
T

trans. (T. Onoma)

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.
 
T

trans. (T. Onoma)

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?

FYI: ruby 1.8.2p2 on Debian Testing
 
M

Minero Aoki

Hi,

In mail "Re: All I want to do is move a directory :("
trans. (T. Onoma) said:
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:

% find
 
T

trans. (T. Onoma)

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:
|
| % find
| .
| ./trash
| ./trash/a
| ./trash/a/test
| ./a
| ./a/test
| ./a/test2
| % mv a trash
| mv: cannot overwrite directory `trash/a'
| % mv -f a trash
| mv: cannot overwrite directory `trash/a'
| % find
| .
| ./trash
| ./trash/a
| ./trash/a/test
| ./a
| ./a/test
| ./a/test2
| %
|
| In my opinion, :force option ensures only that #mv does not
| raise exception, it does not imply continuing process.
|

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.
 
M

Mark Hubbart

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?

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
 
I

Ilmari Heikkinen

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?

-Ilmari
 
T

trans. (T. Onoma)

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?

Hi --

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

T.
 
T

trans. (T. Onoma)

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.

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.
 
B

Bil Kleb

Minero said:
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.

-f, --force do not prompt before overwriting

Why change the convention to which so many are accustomed?

Regards,
 
G

Gennady Bystritsky

'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.

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

-f, --force do not prompt before overwriting

Why change the convention to which so many are accustomed?

Regards,

Sincerely,
Gennady Bystritsky
 
L

Lionel Thiry

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
 
M

Minero Aoki

Hi,

In mail "Re: All I want to do is move a directory :("
trans. (T. Onoma) said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top