# Touch

**URL:** <https://www.ruby-forum.com/t/touch/75995>\
**Category:** Ruby\
**Created:** [November 15, 2006, 12:40am UTC](https://www.ruby-forum.com/t/touch/75995 "2006-11-15T00:40:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![runxc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ec8ea/32.png) [@runxc](https://www.ruby-forum.com/u/runxc)\
**Post date:** [November 15, 2006, 12:40am UTC](https://www.ruby-forum.com/t/touch/75995/1 "2006-11-15T00:40:26Z")

</div>

I need to “touch” a file from my ruby script. (This is a unix-based  
command that updates the timestamp on a file, creating it, if it  
doesn’t already exist.) I’m running on Windows. A collegue found a port  
of “touch” and installed it and called it using “system”, but i would  
rather see a solution that requires less installation.

I am currently using the following code:

class File  
def self.touch(filename)  
File.open(filename, ‘a’){|f| f.puts ’ '}  
end  
end

This implementation is good enough for my present needs (touching an  
xml file), but it has obvious side effects that could be a problem in  
other contexts.

I did find a touch command in Ruby in the ptools package at  
[Miscellaneous Ruby Utility Modules download | SourceForge.net](http://sourceforge.net/projects/ruby-miscutils/) but it did not work.  
(It zeroed the file it touched!)

Any suggestions for a better “touch” in Ruby?

Bret

---

<div class="post-metadata">

**Author:** ![runxc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ec8ea/32.png) [@runxc](https://www.ruby-forum.com/u/runxc)\
**Post date:** [November 15, 2006, 12:54am UTC](https://www.ruby-forum.com/t/touch/75995/2 "2006-11-15T00:54:27Z")

</div>

> ```
> File.open(filename, 'a'){|f| f.puts ' '}
> 
> ```
> 
> Any suggestions for a better “touch” in Ruby?

What’s wrong with FileUtils.touch() ?

[http://corelib.rubyonrails.org/classes/FileUtils.html#M001802](http://corelib.rubyonrails.org/classes/FileUtils.html#M001802)

---

<div class="post-metadata">

**Author:** ![runxc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ec8ea/32.png) [@runxc](https://www.ruby-forum.com/u/runxc)\
**Post date:** [November 15, 2006, 1:10am UTC](https://www.ruby-forum.com/t/touch/75995/3 "2006-11-15T01:10:29Z")

</div>

Philip H. wrote:

> What’s wrong with FileUtils.touch() ?
> 
> [http://corelib.rubyonrails.org/classes/FileUtils.html#M001802](http://corelib.rubyonrails.org/classes/FileUtils.html#M001802)

That looks good. Thanks.

In retrospect, i realize that i should have used fxri first.

Bret
