Error Codes Wiki

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

Informationalerrno

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.