How to Work With Directories in Ruby

Did you know that you can navigate your file system with Ruby?

With the Ruby “Dir” class.

You can list directory entries, change your current directory & even create new folders!

Here’s an example:

filenames = Dir.entries(".")

This entries method returns an array of filename entries. Every entry is a string, so you’ll need to combine this with the Ruby File class if you want to actually read these files.

Btw, this dot (.) represents the current directory.

This will be the directory your code is running from, NOT the directory you’re (in your terminal) when running the code.

Because of that…

It can be helpful to check your current directory by using the Dir.pwd method.


Let’s learn how to use the Dir class to create new directories (mkdir), rename them (mv), and find file names that follow a specific pattern (glob).

Using Ruby’s Mkdir Method To Create A New Directory

If you want to create a new folder with Ruby you can use the Dir.mkdir method.



If given a relative path, this directory is created under the current path (Dir.pwd).

You can get a few errors:

  • Directory already exists (Errno::EEXIST)
  • Permission denied (Errno::EACCES)
  • You’re trying to create a folder under another folder that doesn’t exist yet (Errno::ENOENT)

The last error usually happens when you’re trying to create nested directories.

Or if you’re using an absolute path that doesn’t exist.

Two solutions:

  • Check if the directory exists before creating it (with Dir.exists?)
  • Use a more advanced class (next section)

Let’s keep learning!

Advanced Operations With The FileUtils Module

If you need an extra bit of horsepower you can bring in the FileUtils module from the standard library.

It includes methods like mkdir_p that create nested directories in one step.

Here’s an example:

require 'fileutils'


Pretty cool, right?

That’s not all, FileUtils also brings extra options for all file operations in the form of keyword arguments. Like the verbose option (prints Linux commands) & the noop (don’t change files) option.

Give it a try!

How to Rename Directories

Ruby allows you to do every operation you can do from your Operating System user interface, or from a terminal.

For example…

You can rename a directory like this:"/tmp/a", "/tmp/b")

You’ll also need to use FileUtils here because mv is not available on the Dir class.

How to Change Your Current Directory

Because all directory operations are run from the current directory, you may want to change it.

You can use the Dir.chdir method to do this.


Dir.chdir("/tmp") { Dir.entries(".") }

This works in two ways:

  • With a block, the current directory changes only for the code inside the block
  • Without a block, it changes for all the code after the method call

Notice that chdir only works within your Ruby process, it doesn’t affect the “outside world”.

In other words…

It won’t change your shell’s working directory after your Ruby program stops running.

Listing Files & Directories With Pattern Matching

Want to find all Ruby files in a folder? An easy task with the glob method!



You can use any other extension you want, like “.txt”, or “.yml”. Or any other text that’s part of the file name.

Want to find files inside all folders?

Yep, it’s possible:


The result is an array with all the file names, including the relative path. You can remove the path & get only the file name by using the File.basename method on every file of the list that you get from glob.


You have learned how to work with directories in Ruby using the Dir & FileUtils classes.

Now it’s your turn to put this into practice.

Thanks for reading! 🙂

laynier says 11 months ago

Very useful and easy to understand! thanks

    Jesus Castello says 11 months ago

    Thanks for reading! 🙂

Promise Chukwuenyem says 11 months ago

I found this piece very helpful. Thank you.

    Jesus Castello says 11 months ago

    Thanks for your feedback! 🙂

Comments are closed