6

I'm not sure how this happened, but I have a number of files that have becomed symlinked to themselves. It seems likely that there won't be any way to restore the files, but hopefully there is. Here is what ls -l says

    lrwxrwxrwx  1 bob users   50 Sep  9 21:45 background.png -> /path/to/background.png

I tried unlinking one of the files, but unfortunately the file disappeared. I've also tried readlink. Readlink says that the path to the file is /path/to/background.png

Like I said, I really don't know how this happened. I am inheriting all these files from a previous admin. Is there any recourse?

15

If a file is symlinked to itself then there's no data present and any attempt to access it will result in a loop, and ultimately an error

eg

$ ls -l myfile 
lrwxrwxrwx 1 sweh sweh 19 Sep  9 22:38 myfile -> /path/to/here/myfile

$ cat myfile 
cat: myfile: Too many levels of symbolic links

Since there's no data, deleting these symlinks won't lose any data, because there is no data to preserve.

If you don't get the Too many levels of symbolic links error when you try to cat the file then your file is not a link to itself.

3

ln will never overwrite a file with a link to itself. You can get a file linked to itself with:

$ ln -s `pwd`/myfile myfile
$ ls -l myfile
lrwxrwxrwx 1 grochmal users 25 Sep 10 03:41 myfile -> /home/grochmal/tmp/myfile

But if you try with a file that contains data:

$ rm -f myfile
$ echo yay > myfile
$ ln -sf `pwd`/myfile myfile
ln: '/home/grochmal/tmp/myfile' and 'myfile' are the same file

ln, on the other hand, will overwrite a file with a link to another file.

Yet, you can be rather sure that a link to itself is a file that never ever had data (or was explicitly deleted before the creation of the soft link).

(on Linux and using ln from coreutils, that is)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.