Create a symbolic link with the name 'dir/file' pointing toward 'target_file'? [closed]

StackOverflow https://stackoverflow.com/questions/21442810

  •  04-10-2022
  •  | 
  •  

Pergunta

I'm thinking no... but:

If you have file ~/foo/foo.jpg and move and rename it to ~/bar.jpg, is there any way to make a symbolic link (with the original name and structure) inside ~/ and point toward ~/bar.jpg?

~/foo$ ls
foo.jpg
~/foo$ mv foo.jpg ~/bar.jpg
~/foo$ cd ~
    ~$ ls
bar.jpg
    ~$ ln -s bar.jpg 'foo/foo.jpg'
    ~$ ls -l
bar.jpg
foo/foo.jpg -> bar.jpg

If there is no way as described above, I can just create a hardlink ~/bar.jpg point toward ~/foo/foo.jpg. But I'd rather not have the directory foo or file foo.jpg at all; just a symbolic link in their place.

Foi útil?

Solução 2

Edit: After clarification, your question might be reworded as: "Can a symlink name (or any Unix/Linux filename) contain a forward slash /?"

The answer is: No. The forward slash / is hard-wired at the file system level as a path separator, separating directories and their content.

But maybe this can help you:

In some cases, symlinks to directories are useful. The following existing path

/a/b/c/d

could be symlinked into /somewhere

$ ln -s /a /somewhere

so that /somewhere/a/b/c/d becomes a valid filesystem path:

$ ls /somewhere/
a
$ ls /somewhere/a/
b
# ... etc

I think that's as close as you can get.

Outras dicas

My Unix skills are shaky, but from my understanding of symbolic links, it is impossible to make a symbolic link follow a renamed file. This is because a symbolic link basically just points to the name of a file, and doesn't know anything about it; when the file it points is moved/deleted, it just points to the name of a file that doesn't exist anymore.

As you mentioned, you can solve your problem by creating a hardlink.

Reference: http://www.nixtutor.com/freebsd/understanding-symbolic-links/

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top