hard links and Symbolic Links

本文探讨了Linux系统中文件的硬链接与符号链接的区别。硬链接指向同一个inode,更改其中一个链接会影响所有链接;而符号链接则指向目标文件路径名,独立于目标文件的inode。文章还介绍了如何创建和管理这些链接。

摘自《Linux programming by example》(作者:Arnold Robbins)

Hard links

When a file is created with open()or creat(), the system finds an unused inode and assigns it to the new file. It
creates the directory entry for the file, with the file's name and inode number in it. The -ioption to lsshows the
inode number:
$ echo hello, world > message  Create new file
$ ls -il message  Show inode number too

228786 -rw-r--r-- 1 arnold devel 13 May 4 15:43 message

Since directory entries associate filenames with inodes, it is possible for one file to have multiple names. Each
directory entry referring to the same inode is called a link, or hard link, to the file. Links are created with the ln

command. The usage is 'ln oldfile newfile':

$ ln message msg  Create a link
$ cat msg  Show contents of new name
hello, world
$ ls -il msg message  Show inode numbers
228786 -rw-r--r-- 2 arnold devel 13 May 4 15:43 message

228786 -rw-r--r-- 2 arnold devel 13 May 4 15:43 msg

The output shows that the inode numbers of the two files are the same, and the third field in the long output is now
2. This field is the link count, which reports how many links (directory entries referring to the inode) the file has.
It cannot be emphasized enough: Hard links all refer to the same file. If you change one, you have changed the

others:

$ echo "Hi, how ya doin' ?" > msg  Change file by new name
$ cat message  Show contents by old name
Hi, how ya doin' ?
$ ls -il message msg  Show info. Size changed
228786 -rw-r--r-- 2 arnold devel 19 May 4 15:51 message
228786 -rw-r--r-- 2 arnold devel 19 May 4 15:51 msg

Although we've created two links to the same file in a single directory, hard links are not restricted to being in the

same directory; they can be in any other directory on the same filesystem.

Additionally, you can create a link to a file you don't own as long as you have write permission in the directory in
which you're creating the link. (Such a file retains all the attributes of the original file: the owner, permissions, and
so on. This is because it isthe original file; it has only acquired an additional name.) User-level code cannot create

a hard link to a directory.

Once a link is removed, creating a new file by the same name as the original file creates a new file:
$ rm message  Remove old name
$ echo "What's happenin?" > message  Reuse the name
$ ls -il msg message  Show information
228794 -rw-r--r-- 1 arnold devel 17 May 4 15:58 message
228786 -rw-r--r-- 1 arnold devel 19 May 4 15:51 msg

Notice that the link counts for both files are now equal to 1.

Symbolic Links

We started the chapter with a discussion of partitions, filesystems, and inodes. We also saw that directory entries
associate names with inode numbers. Because directory entries contain no other information, hard links are
restricted to files within the same filesystem. This has to be; there is no way to distinguish inode 2341on one
filesystem from inode 2341on another filesystem. Here is what happens when we try:
$ mount  Show filesystems in use
/dev/hda2 on / type ext3 (rw)
/dev/hda5 on /d type ext3 (rw)
...
$ ls -li /tmp/message  Earlier example was on filesystem for /
228786 -rw-r--r-- 2 arnold devel 19 May 4 15:51 /tmp/message
$ cat /tmp/message
Hi, how ya doin' ?
$ /bin/pwd  Current directory is on a different filesystem
/d/home/arnold
$ ln /tmp/message .  Attempt the link

ln: creating hard link `./message' to `/tmp/message': Invalid cross-device link


A symbolic link (also referred to as a soft
link) is a special kind of file (just as a directory is a special kind of file). The contents of the file are the pathname
of the file being "pointed to." All modern Unix systems, including Linux, provide symbolic links; indeed they are

now part of POSIX.

Symbolic links have their disadvantages:

  • They take up extra disk space, requiring a separate inode and data block. Hard links take up only a
directory slot.
  • They add overhead. The kernel has to work harder to resolve a pathname containing symbolic links.
  • They can introduce "loops." Consider the following:
$ rm -f a b  Make sure 'a' and 'b' don't exist
$ ln -s a b  Symlink old file 'a' to new file 'b'
$ ln -s b a  Symlink old file 'b' to new file 'a'
$ cat a  What happens?

cat: a: Too many levels of symbolic links
The kernel has to be able to detect this case and produce an error message.
  • They are easy to break. If you move the pointed-to file to a different location or rename it, the symbolic link
is no longer valid. This can't happen with a hard link.

在自媒体领域,内容生产效率与作品专业水准日益成为从业者的核心关切。近期推出的Coze工作流集成方案,为内容生产者构建了一套系统化、模块化的创作支持体系。该方案通过预先设计的流程模块,贯穿选题构思、素材整理、文本撰写、视觉编排及渠道分发的完整周期,显著增强了自媒体工作的规范性与产出速率。 经过多轮实践验证,这些标准化流程不仅精简了操作步骤,减少了机械性任务的比重,还借助统一的操作框架有效控制了人为失误。由此,创作者得以将主要资源集中于内容创新与深度拓展,而非消耗于日常执行事务。具体而言,在选题环节,系统依据实时舆情数据与受众偏好模型生成热点建议,辅助快速定位创作方向;在编辑阶段,则提供多套经过验证的版式方案与视觉组件,保障内容呈现兼具美学价值与阅读流畅性。 分发推广模块同样经过周密设计,整合了跨平台传播策略与效果监测工具,涵盖社交网络运营、搜索排序优化、定向推送等多重手段,旨在帮助内容突破单一渠道局限,实现更广泛的受众触达。 该集成方案在提供成熟模板的同时,保留了充分的定制空间,允许用户根据自身创作特性与阶段目标调整流程细节。这种“框架统一、细节可变”的设计哲学,兼顾了行业通用标准与个体工作习惯,提升了工具在不同应用场景中的适应性。 从行业视角观察,此方案的问世恰逢其时,回应了自媒体专业化进程中对于流程优化工具的迫切需求。其价值不仅体现在即时的效率提升,更在于构建了一个可持续迭代的创作支持生态。通过持续吸纳用户反馈与行业趋势,系统将不断演进,助力从业者保持与行业发展同步,实现创作质量与运营效能的双重进阶。 总体而言,这一工作流集成方案的引入,标志着自媒体创作方法向系统化、精细化方向的重要转变。它在提升作业效率的同时,通过结构化的工作方法强化了内容产出的专业度与可持续性,为从业者的职业化发展提供了坚实的方法论基础。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值