Linux errno 17 EEXIST — File Exists Error
About Linux errno 17 EEXIST
Linux errno 17 (EEXIST) occurs when a program tries to create a file or directory that already exists, common with mkdir, link, and mkfifo system calls. 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 is errno 17 on most Linux systems. Triggered by system calls that expect to create a new object. mkdir() fails with EEXIST if the directory already exists. open() with O_CREAT | O_EXCL fails if the file exists. Common in scripts that do not check existence before creation. Understanding these fundamentals will help you diagnose and resolve this issue more effectively.
The most common reasons this occurs include: Script running mkdir without checking if directory exists. Race condition where multiple processes create the same file. Package installation trying to create existing directories. Symlink creation when target already exists. Lock file already present from a previous process. Identifying the root cause is the first step toward finding the right solution.
To resolve this, follow these recommended steps: Use mkdir -p to create directories without error if they exist. In scripts, check existence first: [ -d /path ] || mkdir /path. For lock files, check and remove stale locks before creating. Use ln -sf to force-create symlinks even if they exist. In C/C++, handle EEXIST in error handling after system calls. 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
Is EEXIST always errno 17?
On Linux and most POSIX systems, yes. It is defined in errno.h as 17.
Overview
Linux errno 17 (EEXIST) occurs when a program tries to create a file or directory that already exists, common with mkdir, link, and mkfifo system calls.
Key Details
- EEXIST is errno 17 on most Linux systems
- Triggered by system calls that expect to create a new object
- mkdir() fails with EEXIST if the directory already exists
- open() with O_CREAT | O_EXCL fails if the file exists
- Common in scripts that do not check existence before creation
Common Causes
- Script running mkdir without checking if directory exists
- Race condition where multiple processes create the same file
- Package installation trying to create existing directories
- Symlink creation when target already exists
- Lock file already present from a previous process
Steps
- 1Use mkdir -p to create directories without error if they exist
- 2In scripts, check existence first: [ -d /path ] || mkdir /path
- 3For lock files, check and remove stale locks before creating
- 4Use ln -sf to force-create symlinks even if they exist
- 5In C/C++, handle EEXIST in error handling after system calls