How to Fix “Cannot Remove Symbolic Link: Not a Directory” Error on Network Drives


10 views

When working with symbolic links on network-mounted drives (particularly Samba shares), you might encounter a situation where:

ln -s test_dir ~/netdrive/test_dir/

creates a problematic link that appears as either:

l????????? ? ?       ?       ?            ? my_link

or

lrwxrwxrwx 1 username username 8 Mar 20 16:34 my_link

The core issue stems from:

  • Creating a symbolic link pointing to itself (circular reference)
  • Network filesystem (Samba) quirks in handling broken links
  • Filesystem cache inconsistencies showing different ls outputs

Method 1: Force Removal with Find

find . -type l -name "test_dir" -exec rm -f {} +

Method 2: Low-level Unlink

unlink test_dir

If that fails with ENOTDIR:

strace unlink test_dir 2>&1 | grep ENOTDIR

Method 3: Manual Inode Removal

First identify the inode:

ls -i test_dir
123456 test_dir

Then remove it directly:

find . -inum 123456 -delete

For Samba-mounted drives:

  1. Unmount and remount the network drive
  2. Try removing from the server directly
  3. Use smbclient for direct manipulation:
smbclient //server/share
del test_dir
  • Always verify ln command syntax: ln -s TARGET LINK_NAME
  • Use absolute paths for network drive links
  • Implement alias in bashrc:
alias lns='ln -sv'  # Verbose symbolic links

Remember that network filesystems may require cache clearing or remounting to properly reflect changes to broken links.


While working on a Samba-mounted network drive on CentOS, I encountered a particularly stubborn symbolic link that refused to be deleted. The situation emerged after accidentally reversing parameters in multiple ln -s commands:

ln -s test_dir ~/netdrive/test_dir/
ln -s test_dir ~/netdrive/test_dir
ln -s test_dir/ ~/netdrive/test_dir/

The broken link manifests in two distinct ways when viewed with ls -l:

l????????? ? ?       ?       ?            ? my_link
# OR
lrwxrwxrwx 1 username username 8 Mar 20 16:34 my_link

Standard removal attempts fail with:

rm: cannot remove test_dir': Not a directory
unlink: cannot unlink test_dir': Not a directory

The Samba network mount introduces additional complexity. The strace output reveals the system believes we're trying to remove a directory, not a symlink:

newfstatat(AT_FDCWD, "test_dir", 0x7fff71464640, AT_SYMLINK_NOFOLLOW) = -1 ENOTDIR
unlink("test_dir") = -1 ENOTDIR (Not a directory)

After extensive testing, these methods proved successful for removing such broken symlinks on network mounts:

# Method 1: Force removal with find
find -L . -name "test_dir" -type l -delete

# Method 2: Using rsync (works well for network filesystems)
mkdir empty_dir
rsync -av --delete empty_dir/ ~/netdrive/test_dir/

# Method 3: Direct inode manipulation (advanced)
ls -i test_dir # Get inode number
find . -inum [INODE_NUMBER] -exec rm -i {} \;

To avoid this situation:

# Always verify paths before creating symlinks
if [ ! -e "$target" ]; then
    echo "Warning: Target $target doesn't exist"
fi

# Consider using absolute paths for network symlinks
ln -s "$(pwd)/test_dir" ~/netdrive/test_dir_proper

For Samba-specific cases, mounting with these options may help:

//server/share /mnt/netdrive cifs username=user,password=pass,uid=$(id -u),nobrl,nostrictsize,cache=none 0 0