How to create a link in Linux
来源:互联网 发布:轩辕剑符鬼突破数据 编辑:程序博客网 时间:2024/05/22 08:31
查漏补缺旧知识:
How to create a link in Linux
In your Linux file system, a link is a connection between a file name and the actual data on the disk. There are two main types of links that can be created: "hard" links, and "soft" or symbolic links. Hard links are low-level links which the system uses to create elements of the file system itself, such asfiles anddirectories.
Users typically do not want to create or modify hard links themselves, but symbolic links are a useful tool for any Linux user. A symbolic link is a special file that points to another file or directory, which is called thetarget. Once created, a symbolic link can be treated as if it is the target file, even if it is has a different name and is located in another directory. Multiple symbolic links can even be created to the same target file, allowing the target to be accessed by multiple names.
The symbolic link is a file in its own right, but it does not contain a copy of the target file's data. It is similar to ashortcut inMicrosoft Windows: if you delete a symbolic link, the target is unaffected. Also, if the target of a symbolic link is deleted, moved, or renamed, the symbolic link is not updated. When this happens, the symbolic link is called "broken" or "orphaned", and will no longer function as a link.
How to create symbolic links using a file manager
One easy way to create a symbolic link from within the X Windows GUI is with your file manager. Different Linux distributions use different file managers, but the process is similar. Locate a target file in your file manager GUI, highlight it by clicking on it once, and select the option "create a link." This option is usually found under the Edit menu, or in thecontext menu that appears when youright-click the highlighted file.
In the example shown above, using the Thunar file manager, we have highlighted the filemyfile.txt, then selected Make Link... in the Edit menu. After completed a new symbolic link called link to myfile.txt is created. This link can berenamed or moved to another location, and it will always point to our target (unless the target is later moved or deleted, in which case the link will become orphaned).
How to create symbolic links from the command line
The command line is a powerful tool in Linux because it gives you greater control over your commands. (For more information about the command line, and how to access it from within Linux, see ourLinux and Unix shell tutorial).
You can create symbolic links using the ln command's -s option. The general syntax for creating a symbolic link is:
ln -s target linkname
For instance, if we have a file in our working directory called myfile.txt, and we want to create a symbolic link in the same directory calledmylink, we could use the command:
ln -s myfile.txt mylink
Let's see what this will look like on the command line. Here, we have opened aterminal session that places us at ourshell'scommand prompt. We are logged in a system namedmyhost as a user named user, and our working directory is a folder in ourhome directory calledmyfolder:
First, let's use ls with the -l option to produce a long list of all the files in our directory:
We see our file, myfile.txt, which is the only file in the directory. ("total 4" refers to how manyblocks on the disk are used by the files listed, not the total number of files).
Let's use the cat command to view the contents of myfile.txt:
Now, let's create a symbolic link to mylink.txt calledmylink using the ln -s command:
It seems like nothing happened, but this means it worked as expected. If there was an error, or if an unexpected condition was encountered, we would receive a notification.
Now, if we do another ls -l, we see two files — our target and our link:
One of the benefits of doing a long listing with "-l" is that we see extra information in addition to the file name. Notice the "l" at the beginning of the line containing our link name, indicating that the file is a symbolic link. Also, notice that after mylink is the "->" symbol, followed by the name of the target. In our above example the color blue; most shells, by default, are configured to display certain file types in different colors, but your terminal might show different colors or none at all.
Now let's use our symbolic link. If we run cat on it, it displays the contents ofmyfile.txt:
We can rename our link with mv, and it still points to the same target:
But what happens if we move our link somewhere else? In this case, our link will break. We can see this by making a new directory withmkdir, and moving the link into the new directory withmv:
You can see that when we view the contents of directory newfolder withls -l, our link is highlighted in red, indicating that it points to a file that is not there. And if we try tocat the contents of the link, the shell informs us that the file does not exist. This error occurs because the link points to "myfile.txt" with no otherpath information. Therefore, the operating system will always look formyfile.txt in whatever directory the link happens to be in at the moment.
Let's start over by removing newfolder and its contents using the command rm -r:
This time, let's create the symbolic link using the absolute path to myfile.txt. Let's double check the name of our working directory withpwd:
Our working directory is /home/user/myfolder, so let's include this in the target name when we create the link:
As you can see from the output of ls -l, our link now points to the file/home/user/myfolder/myfile.txt. With this path information, we can move the link to another location, and it will still point to our target:
Tip: Your bash shell keeps an environment variable called $PWD that always stores the value of your working directory. You can use this variable to insert the full path before your target name, as long as the target is in your working directory. We can view the value of $PWD using the echo command:
This text is inserted if we use $PWD as part of a command. It is a good idea to enclose it in quotes as"$PWD" in case the directory name has any spaces. The quotes make sure the shell knows they are part of the pathname and not command separators.
Here is our command, and a directory listing to show that it worked:
Notice that we also put a slash ("/") directly between"$PWD" and myfile.txt to complete the full path name.
Additional information
- How do I get into the command line shell?
- ln command description, syntax, options, and examples
转自https://www.computerhope.com/issues/ch001638.html
- How to create a link in Linux
- FND:How to create a link in IE favorite to directly login into apps
- How to customize a link at your desktop in LINUX
- 如何创建软链接 how to create a symbolic link in clearcase
- How to create a user in PostgreSQL
- How to: Create a Binding in Code
- How to create a function in SQL
- How-to create a Windows 10 usb installation media in Linux
- How to: Create a C# Test Project Add-In -wisdom
- How to Create a Slick and Clean Button in Photoshop
- how to create a global singleton object in exe
- How to create SVAs in a seperate file
- How to create a simple Ad Hoc Reporting in HR ?
- How to create a Toastmasters speech in 30 minutes
- How to create a File-Compare function in Visual C#
- How to Create a Second Level GridView in ASP.NET
- How to create a Gantt chart in SharePoint
- how to create a thread in win32 platform
- 【笔记】shiro中的Password加密设置:
- 红黑树(RBTree)的实现
- 字符串的分解问题
- Json进一步应用
- 汇编语言showStr测试子程序
- How to create a link in Linux
- 【BFS瞎搜】九宫重排
- LeetCode刷题(C++)——Verify Preorder Serialization of a Binary Tree(Medium)
- CUBEMX stm32f407 使用(USB虚拟串口)
- B骑士旅行
- 常用网站
- RN 配置的坑2
- 2.NIO入门
- 构造函数不能声明为虚函数, 析构函数可以