Error Codes Wiki

Linux EEXIST (errno 17) — File or Resource Already Exists (Deep Dive)

Informationalerrno

About Linux EEXIST (errno 17)

Deep dive into Linux EEXIST covering mkdir/symlink conflicts, atomic file creation, lock file patterns, and handling EEXIST in scripts and applications. This guide covers everything you need to know about this topic, including common causes, step-by-step solutions, and answers to frequently asked questions.

Here are the key things to understand: EEXIST (errno 17) occurs when creating a file, directory, or resource that already exists. Common with mkdir, symlink, link, mknod, and open with O_CREAT|O_EXCL flags. O_CREAT|O_EXCL is the standard pattern for atomic lock file creation. In shell: mkdir -p avoids EEXIST by not failing if directory already exists. Socket bind() returns EEXIST if the address is already bound (actually EADDRINUSE on Linux). Understanding these fundamentals will help you diagnose and resolve this issue more effectively.

The most common reasons this occurs include: Creating a directory that already exists without -p flag. Creating a symbolic link where one already exists. Lock file from a previous process instance still present. Race condition: two processes trying to create the same resource simultaneously. Package installation trying to create a file that already exists from another package. Identifying the root cause is the first step toward finding the right solution.

To resolve this, follow these recommended steps: For directories: use mkdir -p which succeeds even if the directory exists. For symlinks: remove first, then create: rm -f /path/to/link && ln -s target /path/to/link. For lock files: check if the owning process is still running (check PID in lock file). For atomic creation: use open(path, O_CREAT|O_EXCL) which fails atomically if file exists. Use ln -sf for force-creating symlinks (removes existing link first). If these steps do not resolve the issue, consider consulting additional resources or a qualified professional.

This article is part of our Linux Error Codes collection on Error Codes Wiki. We provide comprehensive, up-to-date information to help you find solutions quickly.

Quick Answer

How do I create a directory only if it does not exist?

Use mkdir -p /path/to/dir. The -p flag creates parent directories as needed and does not error if the directory exists.

Overview

Deep dive into Linux EEXIST covering mkdir/symlink conflicts, atomic file creation, lock file patterns, and handling EEXIST in scripts and applications.

Key Details

  • EEXIST (errno 17) occurs when creating a file, directory, or resource that already exists
  • Common with mkdir, symlink, link, mknod, and open with O_CREAT|O_EXCL flags
  • O_CREAT|O_EXCL is the standard pattern for atomic lock file creation
  • In shell: mkdir -p avoids EEXIST by not failing if directory already exists
  • Socket bind() returns EEXIST if the address is already bound (actually EADDRINUSE on Linux)

Common Causes

  • Creating a directory that already exists without -p flag
  • Creating a symbolic link where one already exists
  • Lock file from a previous process instance still present
  • Race condition: two processes trying to create the same resource simultaneously
  • Package installation trying to create a file that already exists from another package

Steps

  1. 1For directories: use mkdir -p which succeeds even if the directory exists
  2. 2For symlinks: remove first, then create: rm -f /path/to/link && ln -s target /path/to/link
  3. 3For lock files: check if the owning process is still running (check PID in lock file)
  4. 4For atomic creation: use open(path, O_CREAT|O_EXCL) which fails atomically if file exists
  5. 5Use ln -sf for force-creating symlinks (removes existing link first)

Tags

linuxeexisterrno-17mkdirlock-file

More in Errno

Frequently Asked Questions

Use mkdir -p /path/to/dir. The -p flag creates parent directories as needed and does not error if the directory exists.