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五种IO模型的使用解读

《Linux五种IO模型的使用解读》文章系统解析了Linux的五种IO模型(阻塞、非阻塞、IO复用、信号驱动、异步),重点区分同步与异步IO的本质差异,强调同步由用户发起,异步由内核触发,通过对比各模... 目录1.IO模型简介2.五种IO模型2.1 IO模型分析方法2.2 阻塞IO2.3 非阻塞IO2.4

Linux中查看操作系统及其版本信息的多种方法

《Linux中查看操作系统及其版本信息的多种方法》在服务器运维或者部署系统中,经常需要确认服务器的系统版本、cpu信息等,在Linux系统中,有多种方法可以查看操作系统及其版本信息,以下是一些常用的方... 目录1. lsb_pythonrelease 命令2. /etc/os-release 文件3. h

linux部署NFS和autofs自动挂载实现过程

《linux部署NFS和autofs自动挂载实现过程》文章介绍了NFS(网络文件系统)和Autofs的原理与配置,NFS通过RPC实现跨系统文件共享,需配置/etc/exports和nfs.conf,... 目录(一)NFS1. 什么是NFS2.NFS守护进程3.RPC服务4. 原理5. 部署5.1安装NF

linux配置podman阿里云容器镜像加速器详解

《linux配置podman阿里云容器镜像加速器详解》本文指导如何配置Podman使用阿里云容器镜像加速器:登录阿里云获取专属加速地址,修改Podman配置文件并移除https://前缀,最后拉取镜像... 目录1.下载podman2.获取阿里云个人容器镜像加速器地址3.更改podman配置文件4.使用po

linux查找java项目日志查找报错信息方式

《linux查找java项目日志查找报错信息方式》日志查找定位步骤:进入项目,用tail-f实时跟踪日志,tail-n1000查看末尾1000行,grep搜索关键词或时间,vim内精准查找并高亮定位,... 目录日志查找定位在当前文件里找到报错消息总结日志查找定位1.cd 进入项目2.正常日志 和错误日

防止Linux rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

linux系统上安装JDK8全过程

《linux系统上安装JDK8全过程》文章介绍安装JDK的必要性及Linux下JDK8的安装步骤,包括卸载旧版本、下载解压、配置环境变量等,强调开发需JDK,运行可选JRE,现JDK已集成JRE... 目录为什么要安装jdk?1.查看linux系统是否有自带的jdk:2.下载jdk压缩包2.解压3.配置环境

Linux搭建ftp服务器的步骤

《Linux搭建ftp服务器的步骤》本文给大家分享Linux搭建ftp服务器的步骤,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录ftp搭建1:下载vsftpd工具2:下载客户端工具3:进入配置文件目录vsftpd.conf配置文件4: