# FileUtils head-scratcher

**URL:** <https://rubytalk.org/t/fileutils-head-scratcher/63686>\
**Category:** ruby-talk\
**Created:** [25 September 2011 16:22 UTC](https://rubytalk.org/t/fileutils-head-scratcher/63686 "2011-09-25T16:22:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chuck\_Remes](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@Chuck\_Remes](https://rubytalk.org/u/Chuck_Remes)\
**Post date:** [25 September 2011 16:22 UTC](https://rubytalk.org/t/fileutils-head-scratcher/63686/1 "2011-09-25T16:22:34Z")

</div>

In one of my projects I need to move all files and subdirectories in a particular directory up on level.

e.g. mv \* ..

When I wrote this in Ruby using FileUtils, it works the first time but subsequent runs fail and I don't understand why. The error doesn't make sense because the file/directory it is complaining about is always already there.

Here's the code:

require 'fileutils'

def makedir(name)  
FileUtils.mkdir name unless File.exists?(name) && File.directory?(name)  
end

makedir('dir')  
Dir.chdir('dir') do  
makedir('subdir')  
Dir.chdir('subdir') do  
&nbsp;&nbsp;&nbsp;5.times { |i| FileUtils.touch i.to\_s }  
&nbsp;&nbsp;&nbsp;makedir('subsubdir')  
&nbsp;&nbsp;&nbsp;Dir.chdir('subsubdir') do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5.times { |i| FileUtils.touch i.to\_s }  
&nbsp;&nbsp;&nbsp;end  
end

subdir = Dir.glob("subdir/\*")  
[FileUtils.mv](http://FileUtils.mv) subdir, '.', :verbose =\> true  
end

The first run works. The second run fails.

cremes$ ruby a.rb  
mv subdir/0 subdir/1 subdir/2 subdir/3 subdir/4 subdir/subsubdir .  
cremes$ ruby a.rb  
mv subdir/0 subdir/1 subdir/2 subdir/3 subdir/4 subdir/subsubdir .  
An exception occurred running a.rb  
&nbsp;&nbsp;&nbsp;&nbsp;File exists - . (Errno::EEXIST)

Backtrace:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ } in [FileUtils.mv](http://FileUtils.mv) at /Volumes/spiff/cremes/dev/ruby/rubinius  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/lib/18/fileutils.rb:500  
&nbsp;&nbsp;{ } in FileUtils.fu\_each\_src\_dest at /Volumes/spiff/cremes/dev/ruby/rubinius  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/lib/18/fileutils.rb:1402  
{ } in FileUtils.fu\_each\_src\_dest0 at /Volumes/spiff/cremes/dev/ruby/rubinius  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/lib/18/fileutils.rb:1411  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Array#each at kernel/bootstrap/array.rb:66  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileUtils.fu\_each\_src\_dest0 at /Volumes/spiff/cremes/dev/ruby/rubinius  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/lib/18/fileutils.rb:1409  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileUtils.fu\_each\_src\_dest at /Volumes/spiff/cremes/dev/ruby/rubinius  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/lib/18/fileutils.rb:1400  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[FileUtils.mv](http://FileUtils.mv) at /Volumes/spiff/cremes/dev/ruby/rubinius  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/lib/18/fileutils.rb:495  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ } in Object#\_\_script\_\_ at a.rb:19  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dir.chdir at kernel/common/dir.rb:68  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object#\_\_script\_\_ at a.rb:8  
&nbsp;&nbsp;&nbsp;&nbsp;Rubinius::CodeLoader#load\_script at kernel/delta/codeloader.rb:65  
&nbsp;&nbsp;&nbsp;&nbsp;Rubinius::CodeLoader.load\_script at kernel/delta/codeloader.rb:107  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Rubinius::Loader#script at kernel/loader.rb:618  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Rubinius::Loader#main at kernel/loader.rb:772

How do I get this to work \*every\* time?

cr

---

<div class="post-metadata">

**Author:** ![Reid\_Thompson1](https://avatars.discourse-cdn.com/v4/letter/r/6de8d8/32.png) [@Reid\_Thompson1](https://rubytalk.org/u/Reid_Thompson1)\
**Post date:** [25 September 2011 16:58 UTC](https://rubytalk.org/t/fileutils-head-scratcher/63686/2 "2011-09-25T16:58:47Z")

</div>

add

:force =\> true

to the call to  
FileUtils.mv subdir, '.', :verbose =\> true

> **···**
>
> On 9/25/2011 12:22 PM, Chuck Remes wrote:
> 
> > require 'fileutils'
> > 
> > def makedir(name)  
> > &nbsp;&nbsp;FileUtils.mkdir name unless File.exists?(name)&& File.directory?(name)  
> > end
> > 
> > makedir('dir')  
> > Dir.chdir('dir') do  
> > &nbsp;&nbsp;makedir('subdir')  
> > &nbsp;&nbsp;Dir.chdir('subdir') do  
> > &nbsp;&nbsp;&nbsp;&nbsp;5.times {|i| FileUtils.touch i.to\_s }  
> > &nbsp;&nbsp;&nbsp;&nbsp;makedir('subsubdir')  
> > &nbsp;&nbsp;&nbsp;&nbsp;Dir.chdir('subsubdir') do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5.times {|i| FileUtils.touch i.to\_s }  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;subdir = Dir.glob("subdir/\*")  
> > &nbsp;&nbsp;FileUtils.mv subdir, '.', :verbose =\> true  
> > end

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [26 September 2011 11:04 UTC](https://rubytalk.org/t/fileutils-head-scratcher/63686/3 "2011-09-26T11:04:55Z")

</div>

Chuck, you should also be aware that you move directories up one level  
as well. Is this what you want?

Kind regards

robert

> **···**
>
> On Sun, Sep 25, 2011 at 6:58 PM, Reid Thompson \<reid.thompson@ateb.com\> wrote:
> 
> > On 9/25/2011 12:22 PM, Chuck Remes wrote:
> > 
> > > require 'fileutils'
> > > 
> > > def makedir(name)  
> > > FileUtils.mkdir name unless File.exists?(name)&& File.directory?(name)  
> > > end
> > > 
> > > makedir('dir')  
> > > Dir.chdir('dir') do  
> > > makedir('subdir')  
> > > Dir.chdir('subdir') do  
> > > 5.times {|i| FileUtils.touch i.to\_s }  
> > > makedir('subsubdir')  
> > > Dir.chdir('subsubdir') do  
> > > 5.times {|i| FileUtils.touch i.to\_s }  
> > > end  
> > > end
> > > 
> > > subdir = Dir.glob("subdir/\*")  
> > > FileUtils.mv subdir, '.', :verbose =\> true  
> > > end
> > 
> > add
> > 
> > :force =\> true
> > 
> > to the call to  
> > FileUtils.mv subdir, '.', :verbose =\> true
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Chuck\_Remes](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@Chuck\_Remes](https://rubytalk.org/u/Chuck_Remes)\
**Post date:** [26 September 2011 12:54 UTC](https://rubytalk.org/t/fileutils-head-scratcher/63686/4 "2011-09-26T12:54:09Z")

</div>

Robert, yes, my original message (first line) said that was my goal.

BTW, adding the :force =\> true option made this work. I am still puzzled as to why (I should probably look at the underlying implementation) but I am satisfied for now.

Thanks all for the help.

cr

> **···**
>
> On Sep 26, 2011, at 6:04 AM, Robert Klemme wrote:
> 
> > On Sun, Sep 25, 2011 at 6:58 PM, Reid Thompson \<reid.thompson@ateb.com\> wrote:
> > 
> > > On 9/25/2011 12:22 PM, Chuck Remes wrote:
> > > 
> > > > require 'fileutils'
> > > > 
> > > > def makedir(name)  
> > > > FileUtils.mkdir name unless File.exists?(name)&& File.directory?(name)  
> > > > end
> > > > 
> > > > makedir('dir')  
> > > > Dir.chdir('dir') do  
> > > > makedir('subdir')  
> > > > Dir.chdir('subdir') do  
> > > > &nbsp;&nbsp;&nbsp;5.times {|i| FileUtils.touch i.to\_s }  
> > > > &nbsp;&nbsp;&nbsp;makedir('subsubdir')  
> > > > &nbsp;&nbsp;&nbsp;Dir.chdir('subsubdir') do  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5.times {|i| FileUtils.touch i.to\_s }  
> > > > &nbsp;&nbsp;&nbsp;end  
> > > > end
> > > > 
> > > > subdir = Dir.glob("subdir/\*")  
> > > > FileUtils.mv subdir, '.', :verbose =\> true  
> > > > end
> > > 
> > > add
> > > 
> > > :force =\> true
> > > 
> > > to the call to  
> > > FileUtils.mv subdir, '.', :verbose =\> true
> > 
> > Chuck, you should also be aware that you move directories up one level  
> > as well. Is this what you want?
