Xiaobai learns Linux (5)-Linux file and directory creation connection

ln —Create link

The ln command is used to create hard links or symbolic links (soft links). Let me briefly explain what is called a hard link and what is called a symbolic link:
hard link:

1. A hard link (also called a link) is one or more file names of a file. Besides, the so-called link is nothing more than linking the file name with the node number used by the computer file system. Therefore, we can use multiple file names to link with the same file, and these file names can be in the same directory or in different directories.
2. A file has several file names (using the ln command to realize multiple file names), we say how many links the file has. From the definition, the number of links can be 1, which means that the file has only one file name.
3. In short, a hard link is to allow multiple file names that are not or in the same directory to be able to modify the same file at the same time. After one of them is modified, all files that have hard links to it are modified together.

Symbolic link (soft link):

1. Symbolic link (soft link) is a special type of file, which contains a reference to other files or directories in the form of absolute path or relative path.
2. The operation of the symbolic link is transparent: the program that reads and writes the symbolic link file will behave directly on the target file. Some programs that require special handling of symbolic links (such as backup programs) may recognize and directly operate on them.
3. A symbolic link file contains only one text string, which is interpreted by the operating system as a path to another file or directory. It is an independent file, its existence does not depend on the target file. If you delete a symbolic link, the target file it points to is not affected. If the target file is moved, renamed or deleted, any symbolic links pointing to it will still exist, but they will point to a file that no longer exists. This situation is sometimes called abandonment.

Explanation and comparison:
Compared to more modern symbolic links, hard links are the original Unix way of creating links. Each file has a hard link by default, and this hard link gives the file name. Every time we create a hard link, we create an additional directory entry for a file. There are two important limitations of hard links:

  1. A hard link cannot be associated with files outside of the file system where it is located. This means that a link cannot be associated with files that are not on the same disk partition as the link itself.
  2. A hard link cannot be associated with a directory.

There is no difference between a hard link and the file itself. Unlike symbolic links, when you list the contents of a directory that contains hard links, you will see that there are no special link instructions. When a hard link is deleted, the link is deleted, but the content of the file itself still exists (that is, the disk space it occupies will not be reallocated), until all links associated with this file are deleted. So the symbols seem to be more commonly used.

Symbolic links are created to overcome the limitations of hard links. Symbolic links are effective by creating a special type of file, which contains a text pointer to an associated file or directory. In this respect, they are similar to Windows shortcuts.

A symbolic link points to a file, and the symbolic link itself is almost indistinguishable from other symbolic links. For example, if you write something to a symbolic link, the associated file is also written. However, when you delete a symbolic link, only the link is deleted, not the file itself. If you delete the file before the symbolic link, the link still exists, but it doesn't point to anything. In this case, this link is called a bad link. In many implementations, the ls command displays broken links in different colors, such as red, to show their existence.

Disadvantages of symbolic links:
because the link file contains the path information of the original file, when the original file is moved from one directory to another directory, and then the link file is accessed, the system will not find it, and the hard link does not have this shortcoming.

Create hard link

ln file link

Insert picture description here
Create symbolic link

ln -s item link

"Item" can be a file or a directory.
Insert picture description here
Insert picture description here
Several options of ln
(some of them are translated by myself, which may not be accurate.)
-b: create a backup file for each existing target file, but do not accept any parameters
-f: forcefully delete any existing target file The target file
-i: prompt whether to delete the destination
-L: dereference the target of the symbolic link
-n: if the link name is a symbolic link pointing to the directory, it will be treated as a normal file
-s: create a symbolic link instead of a hard link

Create a game field (combat exercise)

Create a safe zone first, and then play with file operation commands. Those commands learned before:
cp — copy files and directories
mv — move/rename files and directories
mkdir — create directories
rm — delete files and directories
ln — create hard links and symbolic links

First, we need a working directory. Create a directory called "playground" in my directory.
First make sure we are in the initial directory, and then create
Insert picture description here
the playground directory. Create a pair of directories under the playground directory, called "dir1" and "dir2". Change our current working directory to the playground, and then execute the mkdir command.
Insert picture description here
Next, let's enter some data into our playground. We can do this by copying a file. We use the cp command to copy the passwd file from the /etc directory to the current working directory. At the same time, use a dot at the end of the command to simplify the writing of the current working directory. You can repeat the operation to copy the command. Use the "-v" option to see the detailed operation. It displays a concise message indicating what operation it has performed.
Insert picture description here
Now, the name "passwd" does not look good, so we change it For fun. Then transfer the fun file, by moving the renamed file to each subdirectory, and then move it back to the current directory: first, move the fun file into the directory dir1, then move the fun file from dir1 to the directory dir2, and finally, Bring the fun file back to the current working directory.
Insert picture description here
Next, look at the effect of moving the directory. First, we first move our data files to the dir1 directory, then move dir1 to the dir2 directory, and use ls to confirm the execution result. Note: Because the directory dir2 already exists, the mv command will move dir1 to the dir2 directory. If dir2 does not exist, mv will rename dir1 to dir2. Finally, put everything back in place.
Insert picture description here
Now, we try to create a link. The first is hard links. We create some links that associate our data files.
Insert picture description here
Observe the list after the ls command, the second field of the last two files fun and fun-hard is "4", this number is the number of hard links in the file "fun". Remember that a file has at least one hard link, because the file name is created by the link. So, how do we know that fun and fun-hard are actually the same file? In this example, ls is not very useful. Although we can see that the fun and fun-hard files are the same size (the fifth field), our list does not provide reliable information to determine (the two files are the same). In order to solve this problem, we need to study further.

When considering hard links, we can assume that the file consists of two parts: the data part containing the file content and the file name part. When we create a file hard link, we actually create an additional name part for the file, and these names are all associated with the same data part. At this time, the system will allocate a series of disk blocks to the so-called index node, and then the index node is associated with the file name part. Therefore, each hard link is related to a specific index node that contains the content of the file.
The ls command has a method to display the information of the file index node. The "-i" option is added to the command.
Insert picture description here
The purpose of establishing symbolic links is to overcome the two shortcomings of hard links: hard links cannot span physical devices, and hard links cannot associate directories, only files. Symbolic link is a special type of file, it contains a text pointer to the target file or directory. When we create a symbolic link, we will create a text description of where the target file is associated with the symbolic link. The process of creating a symbolic link is similar to creating a hard link
Insert picture description here
directory dir1. The list of fun-sym indicates that it is a symbolic link, which can be known by the first character "l" in the first field, and it also points to ".../fun" . Relative to the storage location of fun-sym, fun is in its previous directory. Also note that the length of the symbolic link file is 6, which is the number of characters contained in the string ".../fun", not the length of the file pointed to by the symbolic link.

The rm command is used to delete files and directories. We will use it to clean up our playground. First, delete a hard link, then delete the file fun and add the "-i" option. Enter "y" at the prompt to delete the file. At this point the link is broken, pointing to a file that no longer exists.
Insert picture description here
If we try to use a broken link, we will see the following situation. After
Insert picture description here
deleting the symbolic link, delete the playground. Return to the initial directory, and then use the rm command with the option (-r) to delete the directory playground and all the contents of the directory, including subdirectories.
For symbolic links, it’s worth remembering that most file operations performed are on the linked object, not the link itself. The rm command is a special case. When you delete a link, delete the link itself, not the linked object.
Insert picture description here

Use GUI to create symbolic links

The file managers GNOME and KDE both provide a simple and automated way to create symbolic links. In GNOME, when you drag a file, pressing the Ctrl+Shift keys at the same time creates a link instead of copying or moving the file. In KDE, whenever a file is dropped, a small menu will pop up, which will provide options to copy, move, or create linked files.

Guess you like

Origin blog.csdn.net/qq_45884783/article/details/106064373