Linux command (1): ln-create and delete soft and hard links

The Linux ln command is a very important command. Its function is to establish a synchronized link for a file in another location.

When we need to use the same file in different directories, we don’t need to put a file that must be the same in each directory that we need. We just need to put the file in a fixed directory, and then put it in other directories. You can use the lncommand link to link it under the directory under the directory , so there is no need to repeatedly occupy the disk space.

grammar:

 ln [参数][源文件或目录][目标文件或目录]其中参数的格式为

The format of the parameter is:

[-bdfinsvF] [-S backup-suffix] [-V {
    
    numbered,existing,simple}]

[--help] [--version] [--]

Command function:

In the Linux file system, there are so-called links, which we can regard as aliases for archives, and links can be divided into two types: hard link and symbolic link, meaning hard link A file can have multiple names, and the soft link method is to generate a special file whose content points to the location of another file. Hard links exist in the same file system, but soft links can span different file systems.

No matter whether it is a hard link or a soft link, the original file will not be copied, and it will only take up a very small amount of disk space.

Soft link:

  1. Soft links exist in the form of paths. Similar to shortcuts in Windows operating system
  2. Soft links can cross file systems, hard links cannot
  3. Soft link can link a non-existent file name
  4. The soft link can link to the directory

Hard link:

  1. A hard link exists in the form of a copy of the file. But it does not take up actual space.
  2. Do not allow hard links to the directory
  3. Hard links can only be created in the same file system

Command parameters

Required parameters:

  • -b delete, overwrite the previously established link
  • -d allows super users to make hard links to directories
  • -f enforce
  • -i interactive mode, prompts the user whether to overwrite if the file exists
  • -n treat symbolic links as general directories
  • -s 软链接(符号链接)
  • -v shows detailed processing

Select parameters:

  • -S "-S<suffix backup string>" or "–suffix=<suffix backup string>"
  • -V "-V<backup method>" or "–version-control=<backup method>"
  • --Help display help information
  • --Version display version information

Instance

Create a soft link for the file, and create a soft link link2013 for the log2013.log file. If log2013.log is lost, link2013 will become invalid:

ln -s log2013.log link2013

Output:

[root@localhost test]# ll
-rw-r--r-- 1 root bin      61 11-13 06:03 log2013.log
[root@localhost test]# ln -s log2013.log link2013
[root@localhost test]# ll
lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -> log2013.log
-rw-r--r-- 1 root bin      61 11-13 06:03 log2013.log

Create a hard link to the file, create a hard link ln2013 for log2013.log, log2013.log has the same attributes as ln2013

ln log2013.log ln2013

Output:

[root@localhost test]# ll
lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -> log2013.log
-rw-r--r-- 1 root bin      61 11-13 06:03 log2013.log
[root@localhost test]# ln log2013.log ln2013
[root@localhost test]# ll
lrwxrwxrwx 1 root root     11 12-07 16:01 link2013 -> log2013.log
-rw-r--r-- 2 root bin      61 11-13 06:03 ln2013
-rw-r--r-- 2 root bin      61 11-13 06:03 log2013.log

Guess you like

Origin blog.csdn.net/houxiaoni01/article/details/105162629