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
- 1For directories: use mkdir -p which succeeds even if the directory exists
- 2For symlinks: remove first, then create: rm -f /path/to/link && ln -s target /path/to/link
- 3For lock files: check if the owning process is still running (check PID in lock file)
- 4For atomic creation: use open(path, O_CREAT|O_EXCL) which fails atomically if file exists
- 5Use ln -sf for force-creating symlinks (removes existing link first)
Tags
linuxeexisterrno-17mkdirlock-file
More in Errno
linux-errno-1-epermLinux errno 1 (EPERM) — Operation Not Permitted
Warninglinux-errno-2-enoentLinux errno 2 (ENOENT) — No Such File or Directory
Warninglinux-errno-5-eioLinux errno 5 (EIO) — Input/Output Error
Errorlinux-errno-11-eagainLinux errno 11 (EAGAIN) — Resource Temporarily Unavailable
Informationallinux-errno-12-enomemLinux errno 12 (ENOMEM) — Out of Memory
Criticallinux-errno-13-eaccesLinux errno 13 (EACCES) — Permission Denied
WarningFrequently Asked Questions
Use mkdir -p /path/to/dir. The -p flag creates parent directories as needed and does not error if the directory exists.