Linux file Soft links, hard links

Source: Internet
Author: User
Tags symlink

Hard links and soft links for Linux

There are two ways to link: hard links and soft links

In a Linux file system, a file saved in a disk partition, regardless of the type, assigns a number to it, called the index node number (Inode index).

(1) Hard Links: Multiple file names pointing to the same index node

Role: Allows a file to have multiple valid pathname, so that users can establish a hard connection to important files to prevent "accidental deletion" function.

In fact, a hard link is an alias of a file, deleting only one connection does not affect the index node itself and other connections, only when the last connection is deleted, the file's data block and directory connection will be released. That is, the condition that the file is actually deleted is that all the hard connection files associated with it are deleted.

There are two restrictions on hard-link files

1), do not allow to create hard links to the directory

2), you can create a hard link only between files in the same file system

The LN command creates a new hard link for an existing file. This command understands that the first parameter is an existing file and can be followed by one or more additional hard links. Hard links can be in any location.

Create a hard link for an existing file Newfile.text in the/tmp directory Mewfile-link1.text

[Email protected] ~]# cd/tmp

[email protected] tmp]# Touch Newfile.text

[Email protected] tmp]# LN newfile.text/tmp/newfile-link.text

[email protected] tmp]# ll Newfile.text/tmp/newfile-link.text

-rw-r--r--. 2 root root 0 Nov 04:04 newfile.text

-rw-r--r--. 2 root root 0 Nov 04:04/tmp/newfile-link.text

Even if the original file is deleted, the contents of the file remain readable as long as a hard link exists.

[[email protected] tmp]# echo ' Hello,world ' > Newfile.text

[Email protected] tmp]# rm-f Newfile.text

[Email protected] tmp]# Ll/tmp/newfile-link.text

-rw-r--r--. 1 root root 04:10/tmp/newfile-link.text

[Email protected] tmp]# Cat/tmp/newfile-link.text

Hello,world

(2) Soft link

A soft link is also called a symbolic link, which contains the path name of another file. is actually a shortcut in Windows.

Soft links do not have as many restrictions as hard chaining

1) Soft links have their own file attributes and permissions, and so on.

2) You can create a soft link to a nonexistent file or directory.

3) Soft link can cross file system.

4) Soft links can be created on files or directories, and directories can be located in different file systems

5) When you create a soft link, the link count i_nlink not increase.

6) Deleting a soft link does not affect the file being pointed to, but if the original file being pointed to is deleted, then the related soft connection is called the dead link (that is, dangling link, if it is re-created by pointing to the path file, the dead link can be restored to the normal soft link).

Ln-s command to create a soft link

[email protected]/]# Touch Newfile-link2.tetx

[Email protected]/]# ln-s Newfile-link2.tetx/tmp/newfile-symlink.text

[email protected]/]# ll Newfile-link2.tetx/tmp/newfile-symlink.text

-rw-r--r--. 1 root root 0 Nov 04:19 newfile-link2.tetx

lrwxrwxrwx. 1 root root 04:20/tmp/newfile-symlink.text-Newfile-link2.tetx

When the original file is deleted, the linked file still points to the file, but the destination disappears. Soft links to missing files are called "Hung soft Links",

[Email protected]/]# rm-f newfile-link2.tetx

[Email protected]/]# Ll/tmp/newfile-symlink.text

lrwxrwxrwx. 1 root root 04:20/tmp/newfile-symlink.text-Newfile-link2.tetx

[Email protected]/]# Cat/tmp/newfile-symlink.text

Cat:/tmp/newfile-symlink.text:no such file or directory









Linux file Soft links, hard links

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.