Linux and Unix ln command

About ln

ln creates links between files.

Description

ln creates a link to file TARGET with the name LINKNAME. If LINKNAME is omitted, a link to TARGET is created in the current directory, using the name of TARGET as the LINKNAME.

ln creates hard links by default, or symbolic links if the -s (--symbolic) option is specified. When creating hard links, each TARGET must exist.

What Is A Link?

Before we discuss the ln command, let‘s first discuss the link command, as well as what a link is and how it relates to files as we know them.

A link is an entry in your file system which connects a filename to the actual bytes of data on the disk. More than one filename can "link" to the same data. Here‘s an example. Let‘s create a file named file1.txt:

echo "This is a file." > file1.txt

This command echoes the string "This is a file". Normally this would simply echo to our terminal, but the > operator redirects the string‘s text to a file, in this case file1.txt. We can check that it worked by using cat to display the contents of the file:

cat file1.txt
This is a file.

When this file was created, the operating system wrote the bytes to a location on the disk and also linked that data to a filename, file1.txt so that we can refer to the file in commands and arguments. If you rename the file, the contents of the file are not altered; only the information that points to it. The filename and the file‘s data are two separate entities.

Here‘s an illustration of the filename and the data to help you visualize it:

Using The link Command

What the link command does is allow us to manually create a link to file data that already exists. So, let‘s use link to create our own link to the file data we just created. In essence, we‘ll create another file name for the data that already exists.

Let‘s call our new link file2.txt. How do we create it?

The general form of the link command is: "link filename linkname". Our first argument is the name of the file whose data we‘re linking to; the second argument is the name of the new link we‘re creating.

link file1.txt file2.txt

Now both file1.txt and file2.txt point to the same data on the disk:

cat file1.txt
This is a file.
cat file2.txt
This is a file.

The important thing to realize is that we did not make a copy of this data. Both filenames point to the same bytes of data on the disk. Here‘s an illustration to help you visualize it:

If we change the contents of the data pointed to by either one of these files, the other file‘s contents are changed as well. Let‘s append a line to one of them using the >> operator:

echo "It points to data on the disk." >> file1.txt

Now let‘s look at the contents of file1.txt:

cat file1.txt
This is a file.
It points to data on the disk.

... and now let‘s look at the second file, the one we created with the link command:

cat file2.txt
This is a file.
It points to data on the disk.

Both files show the change because they share the same data on the disk. Changes to the data of either one of these files will change the contents of the other.

But what if we delete one of the files? Will both files be deleted?

No. If we delete one of the files, we‘re simply deleting one of the links to the data. Because we created another link manually, we still have a pointer to that data; we still have a way, at the user-level, to access the data we put in there. So if we use the rm command to remove our first file:

rm file1.txt

...it no longer exists as a file with that name:

cat file1.txt
cat: file1.txt: No such file or directory

...but the link to the data we manually created still exists, and still points to the data:

cat file2.txt
This is a file.
It points to data on the disk.

As you can see, the data stays on the disk even after the "file" (which is actually just a link to the data) is removed. We can still access that data as long as there is a link to it. This is important to know when you‘re removing files — "removing" a file just makes the data inaccessible by unlink-ing it. The data still exists on the storage media, somewhere, inaccessible to the system, and that space on disk is marked as being available for future use.

The type of link we‘ve been working with here is sometimes called a "hard" link. A hard link and the data it links to must always exist on the same filesystem; you can‘t, for instance, create a hard link on one partition to file data stored on another partition. You also can‘t create a hard link to a directory. Only symbolic links may link to a directory; we‘ll get to that in a moment.

The Difference Between ln And link

So what about ln? That‘s why we‘re here, right?

ln, by default, creates a hard link just like link does. So this ln command:

ln file1.txt file2.txt

...is the same as the following link command:

link file1.txt file2.txt

...because both commands create a hard link named file2.txt which links to the data of file1.txt.

However, we can also use ln to create symbolic links with the -s option. So the command:

ln -s file1.txt file2.txt

Will create a symbolic link to file1.txt named file2.txt. In contrast to our hard link example, here‘s an illustration to help you visualize our symbolic link:

About Symbolic Links

Symbolic links, sometimes called "soft" links, are different than "hard" links. Instead of linking to the data of a file, they link to another link. So in the example above, file2.txt points to the link file1.txt, which in turn points to the data of the file.

This has several potential benefits. For one thing, symbolic links (also called "symlinks" for short) can link to directories. Also, symbolic links can cross file system boundaries, so a symbolic link to data on one drive or partition can exist on another drive or partition.

You should also be aware that, unlike hard links, removing the file (or directory) that a symlink points to will break the link. So if we create file1.txt:

echo "This is a file." > file1.txt

...and create a symbolic link to it:

ln -s file1.txt file2.txt

...we can cat either one of these to see the contents:

cat file1.txt
This is a file.
cat file2.txt
This is a file.

...but if we remove file1.txt:

rm file1.txt

...we can no longer access the data it contained with our symlink:

cat file2.txt
cat: file2.txt: No such file or directory

This error message might be confusing at first, because file2.txt still exists in your directory. It‘s a broken symlink, however — a symbolic link which points to something that no longer exists. The operating system tries to follow the symlink to the file that‘s supposed to be there (file1.txt), but finds nothing, and so it returns the error message.

While hard links are an essential component of how the operating system works, symbolic links are generally more of a convenience. You can use them to refer, in any way you‘d like, to information already on the disk somewhere else.

Creating Symlinks To Directories

To create a symbolic link to a directory, simply specify the directory name as the target. For instance, let‘s say we have a directory named documents, which contains one file, named file.txt.

Let‘s create a symbolic link to documents named dox. This command will do the trick:

ln -s documents/ dox

We now have a symlink named dox which we can refer to as if it is the directory documents. For instance, if we use ls to list the contents of the directory, and then to list the contents of the symlinked directory, they will both show the same file:

ls documents
file.txt
ls dox
file.txt

When we work in the directory dox now, we will actually be working in documents, but we will see the word dox instead of documents in all pathnames.

Symbolic links are a useful way to make shortcuts to long, complicated pathnames. For instance, this command:

ln -s documents/work/budgets/Engineering/2014/April aprbudge

...will save us a lot of typing; now, instead of changing directory with the following command:

cd documents/work/budgets/Engineering/2014/April

...we can do this, instead:

cd aprbudge

Normally, you remove directories (once they‘re empty) with the rmdir command. But our symbolic link is not actually a directory: it‘s a file that points to a directory. So to remove our symlink, we just use the rm command:

rm aprbudge

This will remove the symlink, but the original directory and all its files are not affected.

ln syntax

ln [OPTION]... TARGET [...] [LINKNAME [...]]

Options

Here are the options that can be passed to the ln command.

--backup[=CONTROL] Use this option to additionally create a backup of each existing destination file. The style of backup is optionally defined by the value of CONTROL. See below for more information.
-b This functions like --backup, but you cannot specify the CONTROL; the default style (simple) is used.
-d, -F, --directory This option allows the superuser to attempt to hard link directories (although it will probably fail due to system restrictions, even for the superuser).
-f, --force If the destination file or files already exist, overwrite them.
-i, --interactive Prompt the user before overwriting destination files.
-L, --logical Dereference TARGETs that are symbolic links. In other words, if you are trying to create a link (or a symlink) to a symlink, link to what it links to, not to the symlink itself..
-n, --no-dereference Treat LINKNAME as a normal file if it is a symbolic link to a directory.
-P, --physical Make hard links directly to symbolic links, rather than dereferencing them.
-r, --relative Create symbolic links relative to link location.
-s, --symbolic Make symbolic links instead of hard links.
-S, --suffix=SUFFIX Use the file suffix SUFFIX rather than the default suffix "~".
-t, --target-directory=DIRECTORY Specify the DIRECTORY in which to create the links.
-T, --no-target-directory Always treat LINKNAME as a normal file.
-v, --verbose Operate verbosely; print the name of each linked file.
--help Display a help message, and exit.
--version Display version information, and exit.

About The --backup Option

When using the --backup (or -b) option, the default file suffix for backups is ‘~‘. You can change this, however, using the --suffix option or setting the SIMPLE_BACKUP_SUFFIX environment variable.

The CONTROL argument to the --backup option specifies the version control method. Alternatively, it can be specified by setting the VERSION_CONTROL environment variable. Here are the values to use for either one:

none, off never make backups (even if --backup is given)
numbered, t make numbered backups.
existing, nil numbered if numbered backups exist, simple otherwise.
simple, never always make simple backups.

If you use -b instead of --backup, the CONTROL method is always simple.

If you specify the -s option (which symlinks), ln ignores the -L and -P options. Otherwise (if you are making hard links), the last option specified controls behavior when a TARGET is a symbolic link. The default is to act as if -P was specified.

ln examples

ln public_html/myfile1.txt

Create a hard link to the file public_html/myfile1.txt in the current directory.

ln -s public_html/myfile1.txt

Create a symbolic link to the file public_html/myfile1.txt in the current directory.

ln -s public_html/ webstuff

Create a symbolic link to the directory public_html named webstuff.

ln -s -b file1.txt file2.txt

Creates a symbolic link to the file file1.txt named file2.txt. If file2.txt already exists, it is renamed to file2.txt~ before the new file2.txt symlink is created.

Related commands

chmod — Change the permissions of files or directories.

link — Create a hard link to a regular file.

ls — List the contents of a directory or directories.

readlink — Print the value of a symbolic link or canonical file name.

unlink — Remove a file.

时间: 2024-10-16 19:55:34

Linux and Unix ln command的相关文章

Linux/hp unix/AIX日常巡检脚本(转)

以下为Linux/hp unix/AIX日常巡检脚本,大家可以参考着进行改写,用于自己的服务器. #!/usr/bin/ksh syserrdate=`date +"%m/%d"`errcount=0STATUS=HOSTS=`hostname`SCRIPT=`basename $0`REPORT="/tmp/report.txt" #FS percentFILESYSTEM_CHECK() {FS=`df -k|sed '1d'|awk 'sub("%&

linux 和unix 的区别

Linux与Unix的区别  某些PC机的Unix和Linux在实现方面相类似.几乎所有的商业Unix版本都基本支持同样的软件.程序设计环境和网络特性.然而,Linux和Unix的商业版本依然存在许多差别.  Linux支持的硬件范围和商业Unix不一样.一般来说,商业Unix支持的硬件多一些,可是Linux支持的硬件也在不断扩大. 突出的是,Linux至少和商用Unix一样稳定.  对许多用户来说,最重要的因素是价格.Linux是免费软件,用户可以从Internet网上下载.如果上网不方便,可

Linux和UNIX

简单介绍一下Linux的由来以及Linux和UNIX的关系 Linux简介: Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程序和网络协议.它支持32位和64位硬件.Linux继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统. Linux操作系统诞生于1991 年的10 月5 日(这是第一次正式向外公布的时间).Linux存在着许多不同的Linux

15 Linux Split and Join Command Examples to Manage Large Files--reference

by HIMANSHU ARORA on OCTOBER 16, 2012 http://www.thegeekstuff.com/2012/10/15-linux-split-and-join-command-examples-to-manage-large-files/ Linux split and join commands are very helpful when you are manipulating large files. This article explains how

【转】什么是Linux,Linux与UNIX的关系

转自http://blog.chinaunix.net/uid-26748064-id-3125522.html 一.老调重谈,什么是Linux ? Linux 是一个计算机操作系统,计算机操作系统有好多,比如 Windows Macos Unix Bsd 等.所以Linux在地位上和Windows是平行的,都是计算机操作系统,这个表述可能不太正确,呵,我自己是明白,可能表达上有困难.毕竟我不是专业计算机出身的.请大家理解理解吧. Linux 出生计划是在1991年(是Linus计划的),他的父

在Linux或者Unix下打开,每一行都会出多出^M这样的字符

Windows上写好的文件,在Linux或者Unix下打开,每一行都会出多出^M这样的字符,这是因为Windows与*nix的换行符不同所致,我们看看文件格式有什么不同. 在Linux下查看文件格式: # file filename # 20140304110001.csv: ISO-8859 text //不带有^M # 20140304110002.csv: ISO-8859 text, with CRLF line terminators //带有^M Windows下处理的文件就带有CR

MD5做为文件名。机器唯一码有电脑的CPU信息和MAC地址,这两个信息需要在linux或unix系统下才能获取吧。

可以采用机器(电脑)唯一码 + 上传IP + 当前时间戳 + GUID ( + 随机数),然后MD5做为文件名.机器唯一码有电脑的CPU信息和MAC地址,这两个信息需要在linux或unix系统下才能获取吧. //获取电脑的CPU信息function OnlyU(){        $a = '';        $b = array();        if(function_exists('exec')){                if(mailto:[email protected]

Linux,unix,cygwin,centeros下的tar压缩解压缩命令详解

Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置.不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的.但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的.为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面. 我们把这

支持多操作系统和多区域设置,包括Windows, MAC, Linux和Unix

Universal Barcode Font Advantage可以在一个单一的字体中生成的多种条形码类型兼容多种操作系统并具有双字节字符集的语言,例如,日文,中文和韩文 具体功能: Patent Pending字体技术能生成高质量的符号. 以一个单一的字体轻松地打印多种条形码类型. 支持多操作系统和多区域设置,包括Windows, MAC, Linux和Unix. 内含TrueType, OpenType, PCL LaserJet soft fonts和PostScript字体版本. 支持A