4

First of all please note that I'm on a shared server and I don't have root access, so anything involving su or sudo would not be possible.

AFAIK ln and Linux should not even bother checking whether the target of a symlink exists or whether the user has permissions to access it. Yet for some reason this happens:

[email protected] [~/tmp/test]# ls -la
total 8
drwxr-xr-x  2 user user 4096 Aug 17 14:52 ./
drwxr-xr-x 13 user user 4096 Aug 17 14:52 ../
[email protected] [~/tmp/test]# ln -s target a
ln: failed to create symbolic link 'a': No such file or directory
[email protected] [~/tmp/test]# ls -la
total 8
drwxr-xr-x  2 user user 4096 Aug 17 14:52 ./
drwxr-xr-x 13 user user 4096 Aug 17 14:52 ../
[email protected] [~/tmp/test]# touch target
[email protected] [~/tmp/test]# ls -la
total 8
drwxr-xr-x  2 user user 4096 Aug 17 14:53 ./
drwxr-xr-x 13 user user 4096 Aug 17 14:52 ../
-rw-r--r--  1 user user    0 Aug 17 14:53 target
[email protected] [~/tmp/test]# ln -s target a
[email protected] [~/tmp/test]# ls -la
total 8
drwxr-xr-x  2 user user 4096 Aug 17 14:53 ./
drwxr-xr-x 13 user user 4096 Aug 17 14:52 ../
lrwxrwxrwx  1 user user    6 Aug 17 14:53 a -> target
-rw-r--r--  1 user user    0 Aug 17 14:53 target
[email protected] [~/tmp/test]#

As you can see ln will refuse to create a symlink to a nonexistent target.

Also, it will refuse to create symlinks to targets that I don't have read permissions to, targets that are in directories that I don't have read permissions to and even files that I do have permissions to I don't have read permissions to any of the parent directories all the way to the root directory.

Also I've tried using Python's os.symlink() function and it experiences the same issue.

I'm really puzzled here.

4
  • 1
    Note this doesn't say target: no such file or directory, it specifically complains about a. The error isn't about target, it's about not being able to create a. Now why would it not be able to create or find a file? Maybe its parent directory has a problem? This is how I would reason and get to telcoM's answer. Alternate possibility: the filesystem hates your link.
    – user10489
    Aug 17, 2023 at 20:14
  • 1
    What's the version of linux kernel and libc? What type of filesystem is ~/tmp/test on (output of df -T . from within that directory for instance) Aug 18, 2023 at 8:03
  • 2
    I'd suspect that's a remote FS mounted from a non-Unix system and where symlinks are emulated on top of some similar feature of that remote system. What happens if you remove target? Is the a symlink also removed? Aug 18, 2023 at 8:13
  • 1
    And what if the target is renamed? Can you make symlinks to files in directories you don't have access to? Aug 18, 2023 at 8:15

1 Answer 1

5

Have you perhaps used another session to delete the directory you are in?

I can reproduce the error message with:

mkdir -p ~/tmp/test
cd ~/tmp/test
<< in another window: rmdir ~/tmp/test >>
ln -s target a
ln: failed to create symbolic link 'a': No such file or directory

In such a situation, even trying to start another shell will cause an error message:

bash
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

In other words, the error message from ln could mean that the target directory no longer exists. The directory is treated like a deleted-but-still-open file: your shell already has it open, so the shell can still access it, but any new processes cannot.

Even cd .. will result in an error:

cd ..
chdir: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
cd ~/tmp
ls
<< directory is empty >>
3
  • 1
    You wouldn't get the same ls -la output though Aug 18, 2023 at 8:04
  • 1
    The OP also say they can create the symlink if they create the target first, which doesn't tie up Aug 18, 2023 at 8:14
  • The directory does exist (otherwise ls wouldn't have worked either).
    – Dan
    Aug 25, 2023 at 10:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .