It was dangerous to specify a relative path when generating a symbolic link

08/04 postscript I received supplements and corrections in the comments, so I will make corrections as appropriate.

Introduction

When I was learning shell commands, I was addicted to symlinks, so I will leave a memorandum. ~~ I don't know if this is a best practice, but use absolute paths as much as possible to generate symlinks. ~~

This is not a best practice as it depends on the occasion. This is because if you want to manage the files and links of the link source collectively, you will be vulnerable to changing the installation location.

What is a symbolic link?

A symbolic link is one of the functions of the file system of the operating system (OS), and is a mechanism that creates another file that points to a specific file or directory so that the main body can be referenced through it. IT Glossary e-Words

We recognize that symbolic links in Linux are like shortcuts in Windows.

The purpose is similar, but the reality is quite different. In addition, Windows also has symbolic links (using a feature called reparse points). Shortcuts are just launchers with information to open the specified file in the app, but symbolic links are files that can be used as an alternative to the specified file. @angel_p_57

Creating a symbolic link

The command used to create a symbolic link is: hoge.jpg is an arbitrary file name, and fuga is an arbitrary directory name. Here, a symbolic link of a file called hoge.jpg is generated in the fuga directory.


$ ln -s hoge.jpg fuga

Note: In this case, "if the fuga directory already exists" it will run as intended. Without the fuga directory, a symbolic link named fuga would be created in the same directory.

If you set $ ln -s hoge.jpg fuga /, it will be a command to be executed if the directory exists. (I referred to the comments I received)

Thing you want to do

~ / Picture / konosuba.jpg symbolic link, I want to generate it in ~ / Picture / foobar.

What I tried

When specified as a relative path (failed)

Suppose the current directory is ~ / Picture.


~/Picture $ ln -s konosuba.jpg foobar

konosuba.jpg is generated in the foobar directory, but First of all, this is a mistake.

The symbolic link of konosuba.jpg that exists in the current directory is not generated in the foobar folder specified as a relative path ... **. ** **

The symbolic link konosuba.jpg generated in the foobar directory is Relative path from your own perspective konosuba.jpg In other words, let's think about going to refer to konosuba.jpg in the same directory.

In other words, it creates a mysterious file called a symbolic link that references itself.

(This is because "konosuba.jpg ", which is the first argument of the shell command, is read as a relative path based on the position of the symbolic link itself.)

When specified as a relative path (successed)

Suppose the current directory is ~ / Picture.


~/Picture $ ln -s ../konosuba1.jpg foobar

konosuba.jpg is generated in the foobar directory. This is the correct answer. The symbolic link konosuba.jpg generated in the foobar directory is It is generated as a link that refers to "../konosuba1.jpg " when viewed from its own position.

It may not be a problem if you get used to it, but honestly I felt confused.

In that case, use the option -r (--relative). The command will automatically determine the relative position. With ln -s hoge.jpg fuga /, the created symbolic link fuga / hoge.jpg causes a circular reference and is useless, but with ln -s -r hoge.jpg fuga /, fuga / hoge. It will create a suitable link called jpg-> ../hoge.jpg. @angel_p_57

Write with an absolute path


$ ln -s ~/Picture/konosuba.jpg ~/Picture/foobar

With an absolute path, the risk that the referrer is not intended can be greatly reduced. If the directory structure gets complicated, I feel like it's going to be ridiculous, but for the time being, I shouldn't make a mistake ...

in conclusion

I don't feel like I was able to write it well, so I'll make some changes as needed ...

References

Getting Started with Linux Command Line Ubuntu Manuals

Recommended Posts

It was dangerous to specify a relative path when generating a symbolic link
I got stuck when trying to specify a relative path with relative_to () in python
A story about how to specify a relative path in python.
How to put a symbolic link
[Linux] Let's talk about when I stumbled upon a symbolic link I was using.
Convert a path string that uses a symbolic link in the middle to an absolute path
When a voice-analyzed amateur listened to anti-phase music, it was confusing and quiet to visit.
[Selenium] How to specify the relative path of chromedriver?
When I tried to install PIL and matplotlib in a virtualenv environment, I was addicted to it.
[C language] Close () It is dangerous to retry when it fails
A memorandum when I tried to get it automatically with selenium
A note I was addicted to when making a beep on Linux
A note I was addicted to when creating a table with SQLAlchemy
[Ln] How to paste a symbolic link in a directory is complicated