How to Fix “ln: target is not a directory” Error When Creating Symbolic Links in Linux


7 views

When working with Apache web server configurations on Linux systems, you might encounter the error message "ln: target is not a directory" while trying to create symbolic links between your available and enabled sites. Let's examine a typical scenario where this occurs:

sudo ln –s /etc/apache2/sites-available/LoginProject /etc/apache2/sites-enabled/LoginProject

The system responds with:

ln: target /etc/apache2/sites-enabled/LoginProject' is not a directory

The error occurs because the ln command is interpreting the second argument as a directory name where it should create the symlink, rather than as the name for the new symlink itself. Here's how to check the current state:

ls -l /etc/apache2/sites-enabled/LoginProject /etc/apache2/sites-available/LoginProject

Output:

ls: cannot access /etc/apache2/sites-enabled/LoginProject: No such file or directory
-rw-r--r-- 1 root root 526 2011-09-27 18:40 /etc/apache2/sites-available/LoginProject

When creating symbolic links, the proper syntax is:

ln -s TARGET LINK_NAME

In our case, we need to specify the full path for both the target file and the desired symlink name. Here are three correct ways to achieve this:

# Method 1: Full paths
sudo ln -s /etc/apache2/sites-available/LoginProject /etc/apache2/sites-enabled/LoginProject

# Method 2: Relative path from sites-enabled
cd /etc/apache2/sites-enabled
sudo ln -s ../sites-available/LoginProject .

# Method 3: Using a2ensite (Apache-specific tool)
sudo a2ensite LoginProject

1. Directory vs. Filename Confusion:
Remember that if the LINK_NAME exists and is a directory, the symlink will be created inside it. If you want to name the symlink itself, the target shouldn't exist yet.

2. Path Resolution Issues:
Always verify your paths. For Apache configurations, checking directory contents first helps:

ls /etc/apache2/sites-available/
ls /etc/apache2/sites-enabled/

For Apache configurations specifically, Ubuntu/Debian systems provide dedicated tools:

# Enable a site (creates symlink)
sudo a2ensite LoginProject

# Disable a site (removes symlink)
sudo a2dissite LoginProject

# Remember to reload Apache after changes
sudo systemctl reload apache2

After creating the symlink, verify it with:

ls -l /etc/apache2/sites-enabled/

You should see output similar to:

lrwxrwxrwx 1 root root 35 Sep 28 10:15 LoginProject -> ../sites-available/LoginProject

This confirms the symbolic link was created correctly.


When working with symbolic links in Linux, you might encounter the error:

ln: target /path/to/target' is not a directory

This typically occurs when trying to create a symbolic link where the destination path exists but isn't a directory. In the case of Apache configuration:

sudo ln -s /etc/apache2/sites-available/LoginProject /etc/apache2/sites-enabled/LoginProject

Apache's sites-enabled directory is meant to contain symbolic links to configuration files in sites-available. The error appears when:

  • The source is a file (LoginProject)
  • The destination path doesn't exist yet
  • The command syntax is incorrect for this use case

For linking individual files (not directories), the proper syntax is:

sudo ln -s /etc/apache2/sites-available/LoginProject /etc/apache2/sites-enabled/

Or to be explicit about the filename:

sudo ln -s /etc/apache2/sites-available/LoginProject /etc/apache2/sites-enabled/LoginProject

After creating the link, verify it with:

ls -l /etc/apache2/sites-enabled/

You should see output similar to:

lrwxrwxrwx 1 root root 36 Mar 1 10:00 LoginProject -> /etc/apache2/sites-available/LoginProject

For Apache specifically, the recommended way is to use:

sudo a2ensite LoginProject
sudo service apache2 reload

This handles the symlink creation and configuration validation automatically.

  • Using dashes instead of hyphens in the command (watch for copy-paste issues)
  • Incorrect file permissions in sites-available
  • Missing execute permissions on parent directories
# Check file existence
ls -la /etc/apache2/sites-available/LoginProject

# Verify directory permissions
ls -ld /etc/apache2/sites-enabled/

# Test with absolute paths
sudo ln -sv /etc/apache2/sites-available/LoginProject /etc/apache2/sites-enabled/