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
- 1Calling Dir.mkdir on a directory that already exists
- 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 existsWhy 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 - /tmpIdempotent mkdirruby
FileUtils.mkdir_p('output/reports') # safe to call multiple timesRescue Errno::EEXISTruby
begin Dir.mkdir(dir) rescue Errno::EEXIST # already exists — that's fine end
Same error in other languages
Sources
Official documentation ↗
Ruby Core Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev