Linux and Unix Ln command

Source: Internet
Author: User
Tags symlink

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 was created in the current directory, using the name of the Targ ET as the linkname.

Ln creates hard links by default, or symbolic links if the- s (--symbolic) option is Specifie D. 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's a link is and ho W 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 echoeses the string "This isa file". Normally this would simply echoes to our terminal, but the > operator redirects the string ' s text to a file, in This case file1.txt. We can check the it worked by using cat to display the contents of the file:

Cat File1.txt
This is a file.

When this file is created, the operating system wrote the bytes to a location in the disk and also linked that D ATA to a filename, file1.txt So, we can refer to the file in commands and arguments. If You rename the file, the contents of the file is not altered; Only the information this points to it. The filename and the file ' s data are both separate entities.

Here's a illustration of the filename and the data to help you visualize it:

Using the link Command

What the link command does are 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 are 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 are that we do not make a copy of this data. Both filenames point to the same bytes of data on the disk. Here's a illustration to help you visualize it:

If we change the contents of the data pointed to by either one of the these files, the other file ' s contents is changed as we ll. 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 would change the contents of the other.

But what if we delete one of the files? Would 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 has a pointer to that data; We have a-still, 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 this name:

Cat File1.txt
Cat:file1.txt:No such file or directory

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 was actually just a link to the data) is removed. We can still access that data as long as there are a link to it. This was important to know if 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 which space on disk is marked as Bei NG available for future use.

The type of link we ve been working with this 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 could link to a directory; We'll get to this in a moment.

The difference between ln and link

So what's 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

Would 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, is different than "hard" links. Instead of linking to the data of a file, they link to another link. 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 a nother drive or partition.

You should also is aware that, unlike hard links, removing the file (or directory) that a symlink points to would 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 is 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 being there (file1.txt), but finds n Othing, and so it returns the error message.

While hard links is an essential component of the operating system works, symbolic links is generally more of a conv Enience. You can use them to refer and in any-of-the----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 would do the trick:

Ln-s Documents/dox

We now had a symlink named dox which we can refer to as if it was 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'll both show the same file:

LS Documents
File.txt
LS Dox
File.txt

When we are in the directory dox Now, we'll actually be working on documents, but we'll see the WOR D dox instead of documents in all pathnames.

Symbolic links is a useful-in-a-do shortcuts to long, complicated pathnames. For instance, the This command:

Ln-s Documents/work/budgets/engineering/2014/april Aprbudge

. Would 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 we symbolic link is not actually a directory:it ' s a file, points to a directory. So-Remove our symlink, we just use the RM command:

RM Aprbudge

This would remove the symlink, but the original directory and all its files is not affected.

ln syntax
ln [OPTIONTARGET [...] [linkname [...]]
Options

Here is the options that can is passed to the ln command.

--backup [=CONTROL] Use the 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-hard link directories (although it would probably fail due to system restric tions, 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 is symbolic links. In other words, if is trying to create a link (or a symlink) to a symlink, link to what it links to, not to the Symli NK 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 '~'. 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 is 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 do 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 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 CR Eated.

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.

Linux and Unix Ln command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.