Hard link and soft link

Source: Internet
Author: User
In UNIX systems, everything except processes is a file, which is maintained by Linux. To facilitate file management, Linux also introduces the concept of directories (sometimes called folders. Directory enables file classification and management, and the introduction of directories enables Linux file systems to form all files except processes in UNIX systems, while Linux maintains this feature. To facilitate file management, Linux also introduces the concept of directories (sometimes called folders. Directory enables file classification and management. the introduction of directory enables the Linux file system to form a hierarchical directory tree. Listing 1 shows the top-level directory structure of a common Linux system./dev is the directory that stores device-related files. Listing 1. top-level directory structure of Linux
/Root directory ── bin stores the user's binary file ├ ── boot stores the kernel boot configuration file ├ ── dev stores the device file ── etc stores the system configuration file ├ ── home user home directory ├ ── lib dynamic shared Library ── recovery file when the lost + found file system is restored ── media detachable storage media Mount point ├ ── mnt file system temporary mount point ├ ── opt additional application package ├ ── proc system memory ing Directory, provides kernel and process information ── root user home ── sbin stores system binary files ── srv stores service-related data ── sys virtual file system mount point ── tmp stores temporary files ── usr stores user application └ ── var stores changed files such as emails and system logs

Linux, like other UNIX-like systems, does not distinguish between files and directories: directories are files that record other file names. When you use the mkdir command to create a directory, if the name of the directory you want to create is the same as the existing file name (or directory name), the creation will fail.

# ls -F /usr/bin/zi*  /usr/bin/zip*       /usr/bin/zipgrep*  /usr/bin/zipnote*  /usr/bin/zipcloak*  /usr/bin/zipinfo*  /usr/bin/zipsplit*  # mkdir -p /usr/bin/zip  mkdir: cannot create directory `/usr/bin/zip': File exists

 

Linux processes a device as a file. Listing 2 shows how to open the device file/dev/input/event5 and read the file content. File event5 indicates an input device, which may be a mouse or keyboard. View the/proc/bus/input/devices file to check the device type of event5. The device file/dev/input/event5 is read using read () in the form of a forward stream. The struct input_event is defined in the kernel header file linux/input. h.

Listing 2. open and read the device file
 int fd;  struct input_event ie;  fd = open("/dev/input/event5", O_RDONLY);  read(fd, &ie, sizeof(struct input_event));  printf("type = %d  code = %d  value = %d\n",  ie.type, ie.code, ie.value);  close(fd);

Hard Link features

We know that all files have file names and data, which are divided into two parts in Linux: user data and metadata (metadata ). User data, that is, the data block, is the place where the actual content of the file is recorded, and metadata is the additional attribute of the file, such as the file size, creation time, and owner. In Linux, inode numbers in metadata (inode is a part of the file metadata but does not contain the file name, and inode number is the index node number) are the unique identifier of the file, not the file name. The file name is only used to facilitate people's memory and use. the system or program uses the inode number to find the correct file data block. Figure 1. shows the process in which the program obtains the file content through the file name.

Figure 1. open a file by file name

Listing 3. moving or renaming a file
[root@gvtv svnmanager]# stat config.php  File: "config.php"  Size: 2556            Blocks: 8          IO Block: 4096   ???¨????Device: 802h/2050d      Inode: 11804169    Links: 1Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)Access: 2013-10-31 10:10:23.490475413 +0800Modify: 2013-10-31 10:08:40.868475483 +0800Change: 2013-10-31 10:08:40.917475486 +0800[root@gvtv svnmanager]# mv config.php config.php.linux[root@gvtv svnmanager]# stat config.php.linux  File: "config.php.linux"  Size: 2556            Blocks: 8          IO Block: 4096   ???¨????Device: 802h/2050d      Inode: 11804169    Links: 1Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)Access: 2013-10-31 10:10:23.490475413 +0800Modify: 2013-10-31 10:08:40.868475483 +0800Change: 2013-10-31 11:47:20.973471459 +0800[
To view inode numbers in Linux, run the stat or ls-I command (if it is an AIX system, run the istat command ). Listing 3. using the command mv to move and rename the file glibc-2.16.0.tar.xz, the results do not affect the file's user data and inode number, before and after the file movement inode number are: 11804169.

To solve file sharing, two types of links are introduced in Linux: hard link and soft link (also known as symbolic link, soft link or symbolic link ). The link solves the problems of file sharing in Linux. it also brings the benefits of hiding file paths, adding permission security, and saving storage. If an inode number corresponds to multiple file names, these files are called hard links. In other words, hard links use multiple aliases for the same file (see figure 2. hard link is an alias for file, which has a common inode ). The hard link can be created by the command link or ln. Create a hard link to the oldfile as follows.

 link oldfile newfile  ln oldfile newfile

Since hard links have files with the same inode number but different file names, hard links have the following features:

  • Files have the same inode and data block;
  • Only existing files can be created;
  • The file system cannot be used to create hard links;
  • You cannot create directories, but only files;
  • Deleting a hard-link file does not affect other files with the same inode number. When you create a hard link, you create another pointer to the data location, rather than to the existing file. This means that editing a file's hard link is equivalent to editing the file's original instance. The features of soft links are different from those of hard links. if the content in the user data block of a file is pointed to by the path name of another file, the file is a soft connection. Soft links are common files, but the data block content is a little special. Soft links have their own inode numbers and user data blocks (see figure 2 .). Therefore, there are no restrictions on the creation and use of soft links:
    • Soft links have their own file attributes and permissions;
    • You can create soft links to non-existing files or directories;
    • Soft link cross-file system;
    • Soft links can be used to create files or directories;
    • When a soft link is created, the link count I _nlink does not increase;
    • Deleting a soft link does not affect the object to which it is directed. However, if the object to which it is directed is deleted, the soft link is called a dead link (dangling link, if the path file is re-created, the dead link can be restored to a normal soft link ). Figure 2. soft link access

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.