Errno::EEXIST
RubyWARNINGCriticalFilesystem

File or directory already exists

Quick Answer

Use FileUtils.mkdir_p instead of Dir.mkdir to create directories idempotently, or rescue Errno::EEXIST to handle existing paths.

What this means

Raised when an operation requires that a path does not already exist, but it does — for example, creating a directory with Dir.mkdir when the directory is already there. Maps to POSIX errno 17.

Why it happens
  1. 1Calling Dir.mkdir on a directory that already exists
  2. 2Creating a file with exclusive open flags (O_EXCL) on a path that already exists

Fix

Use FileUtils.mkdir_p for idempotent creation

Use FileUtils.mkdir_p for idempotent creation
require 'fileutils'
FileUtils.mkdir_p('path/to/dir')   # creates all intermediate dirs, no error if exists

Why this works

mkdir_p is equivalent to mkdir -p — it succeeds silently if the directory already exists.

Code examples
Reproducing the errorruby
Dir.mkdir('/tmp')
# Errno::EEXIST: File exists - /tmp
Idempotent mkdirruby
FileUtils.mkdir_p('output/reports')   # safe to call multiple times
Rescue Errno::EEXISTruby
begin
  Dir.mkdir(dir)
rescue Errno::EEXIST
  # already exists — that's fine
end
Sources
Official documentation ↗

Ruby Core Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Ruby errors