138

What is the best way to create an empty file in Ruby?

Something similar to the Unix command, touch:

touch file.txt
2

5 Answers 5

205

FileUtils.touch looks like what it does, and mirrors* the touch command:

require 'fileutils'
FileUtils.touch('file.txt')

* Unlike touch(1) you can't update mtime or atime alone. It's also missing a few other nice options.

0
51

If you are worried about file handles:

File.open("foo.txt", "w") {}

From the docs:

If the optional code block is given, it will be passed the opened file as an argument, and the File object will automatically be closed when the block terminates.

5
  • Thanks, I'm sending the new file to another process, so the handle needs to be closed immediately. Nov 11, 2011 at 22:35
  • 16
    Worth pointing out that this doesn't actually mirror touch - it will overwrite the file if it exists. Jan 17, 2013 at 19:32
  • 3
    Use the append mode and the file will not be truncated if it already exists File.open( "foo.txt", "a") {} This also does not mirror 'touch', but then 'touch(1)' was not the question. Aug 5, 2013 at 22:17
  • Using "a" doesn't update the mtime. ("touch" was the subtitle, so Google brings us people who want a Ruby touch.) Aug 15, 2013 at 20:22
  • on *nix systems, Who is the file owner when it is created this way?
    – Islam Azab
    Sep 11, 2014 at 0:21
28

In Ruby 1.9.3+, you can use File.write (a.k.a IO.write):

File.write("foo.txt", "")

For earlier version, either require "backports/1.9.3/file/write" or use File.open("foo.txt", "w") {}

3
  • That's much cleaner, but unfortunately I need to be backwards compatible. I'll keep it in mind for future use though. Nov 11, 2011 at 22:57
  • Overwrites it if already exists, I presume? Good to see you again BTW :)
    – rogerdpack
    Jan 6, 2022 at 1:01
  • @rogerdpack yes, it overwrites if it exists. Mar 19, 2022 at 17:13
4

And also, less advantageous, but very brief:

`touch file.txt`
2
  • 4
    Will fork separate pricess, so is very slow. Feb 15, 2013 at 11:15
  • And is already mentioned in the comments, which I haven't noticed earlier. Feb 15, 2013 at 14:44
-2

Just an example:

File.open "foo.txt", "w"
5
  • 2
    Does this leave an open file handle behind, like it would in some other languages? Nov 11, 2011 at 22:24
  • 5
    @Abhi: It does, but garbage collection will handle it at some point. To avoid that, though, pass an empty block (See Michael Kohl's answer) Nov 11, 2011 at 22:40
  • When is "at some point" going to happen? At the end of the script, or immediately because it hasn't been assigned to any variable? I'm creating the file so other processes on the system can start working with it immediately, and they will be blocked if the file handle is still open for writing. Nov 11, 2011 at 22:49
  • 2
    File.open("foo.txt", "w").close() solves the filehandle issue, but FileUtils.touch is a better solution. Mar 15, 2012 at 17:44
  • Don't leave open files handles like that :-)
    – sekmo
    Jun 7, 2018 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.