How to create a link in Linux

2024-04-10 00:48
文章标签 linux link create

本文主要是介绍How to create a link in Linux,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

查漏补缺旧知识:

How to create a link in Linux

Updated: 04/26/2017 by Computer Hope

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 as files and directories.

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 the target. 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 a shortcut in Microsoft 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 the context menu that appears when you right-click the highlighted file.

Creating symbolic links with a Linux file manager

In the example shown above, using the Thunar file manager, we have highlighted the file myfile.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 be renamed 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 our Linux 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 called mylink, 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 a terminal session that places us at our shell's command prompt. We are logged in a system named myhost as a user named user, and our working directory is a folder in our home directory called myfolder:

First, let's use ls with the -l option to produce a long list of all the files in our directory:

output of command: ls -l

We see our file, myfile.txt, which is the only file in the directory. ("total 4" refers to how many blocks 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:

output of command: cat myfile.txt

Now, let's create a symbolic link to mylink.txt called mylink using the ln -s command:

ln -s myfile.txt mylink

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:

output of command: ls -l, displaying link and target

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 of myfile.txt:

output of cat command

We can rename our link with mv, and it still points to the same target:

Renamed symbolic links still function properly.

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 with mkdir, and moving the link into the new directory with mv:

Moving the link to another location breaks the link, because the link did not specify an absolute path

You can see that when we view the contents of directory newfolder with ls -l, our link is highlighted in red, indicating that it points to a file that is not there. And if we try to cat 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 other path information. Therefore, the operating system will always look for myfile.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:

starting over

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 with pwd:

output of command "pwd"

Our working directory is /home/user/myfolder, so let's include this in the target name when we create the link:

Creating a symbolic link using an absolute path in the target name.

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:

Our symbolic link, which points to an absolute path name, works even when moved to a new location.

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:

Checking the value of the $PWD environment variable

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:

Using the $PWD environment variable to create a symbolic link.

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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/889663

相关文章

Linux系统中查询JDK安装目录的几种常用方法

《Linux系统中查询JDK安装目录的几种常用方法》:本文主要介绍Linux系统中查询JDK安装目录的几种常用方法,方法分别是通过update-alternatives、Java命令、环境变量及目... 目录方法 1:通过update-alternatives查询(推荐)方法 2:检查所有已安装的 JDK方

Linux系统之lvcreate命令使用解读

《Linux系统之lvcreate命令使用解读》lvcreate是LVM中创建逻辑卷的核心命令,支持线性、条带化、RAID、镜像、快照、瘦池和缓存池等多种类型,实现灵活存储资源管理,需注意空间分配、R... 目录lvcreate命令详解一、命令概述二、语法格式三、核心功能四、选项详解五、使用示例1. 创建逻

Linux下在线安装启动VNC教程

《Linux下在线安装启动VNC教程》本文指导在CentOS7上在线安装VNC,包含安装、配置密码、启动/停止、清理重启步骤及注意事项,强调需安装VNC桌面以避免黑屏,并解决端口冲突和目录权限问题... 目录描述安装VNC安装 VNC 桌面可能遇到的问题总结描js述linux中的VNC就类似于Window

linux下shell脚本启动jar包实现过程

《linux下shell脚本启动jar包实现过程》确保APP_NAME和LOG_FILE位于目录内,首次启动前需手动创建log文件夹,否则报错,此为个人经验,供参考,欢迎支持脚本之家... 目录linux下shell脚本启动jar包样例1样例2总结linux下shell脚本启动jar包样例1#!/bin

Linux之platform平台设备驱动详解

《Linux之platform平台设备驱动详解》Linux设备驱动模型中,Platform总线作为虚拟总线统一管理无物理总线依赖的嵌入式设备,通过platform_driver和platform_de... 目录platform驱动注册platform设备注册设备树Platform驱动和设备的关系总结在 l

linux批量替换文件内容的实现方式

《linux批量替换文件内容的实现方式》本文总结了Linux中批量替换文件内容的几种方法,包括使用sed替换文件夹内所有文件、单个文件内容及逐行字符串,强调使用反引号和绝对路径,并分享个人经验供参考... 目录一、linux批量替换文件内容 二、替换文件内所有匹配的字符串 三、替换每一行中全部str1为st

Linux进程CPU绑定优化与实践过程

《Linux进程CPU绑定优化与实践过程》Linux支持进程绑定至特定CPU核心,通过sched_setaffinity系统调用和taskset工具实现,优化缓存效率与上下文切换,提升多核计算性能,适... 目录1. 多核处理器及并行计算概念1.1 多核处理器架构概述1.2 并行计算的含义及重要性1.3 并

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序