Linux ln Command

Linux 命令大全Linux Command Reference

The Linux ln (full English name: link files) command is a very important command. Its function is to create a synchronized link for a file in another location.

When we need to use the same file in different directories, we do not need to put an identical file in every required directory. We only need to place the file in a fixed directory, and then use the ln command to link it in other directories, so there is no need to repeatedly occupy disk space.

Syntax

 ln [参数][源文件或目录][目标文件或目录]
The format of the parameters is as follows:
[-bdfinsvF] [-S backup-suffix] [-V {numbered,existing,simple}]
[--help] [--version] [--]

Command function :
In the Linux file system, there are so-called links. We can regard them as aliases for files. Links can be divided into two types: hard links and symbolic links. A hard link means a file can have multiple names, while a symbolic link is a way to generate a special file whose content points to the location of another file. Hard links exist in the same file system, while symbolic links can span different file systems.

Neither hard links nor symbolic links copy the original file; they only occupy a very small amount of disk space.

Symbolic links:

  • 1. Symbolic links exist in the form of paths. They are similar to shortcuts in the Windows operating system.
  • 2. Symbolic links can cross file systems, but hard links cannot.
  • 3. Symbolic links can link to a file name that does not exist.
  • 4. Symbolic links can link to directories.

Hard links:

  • 1. Hard links exist in the form of file copies. However, they do not occupy actual space.
  • 2. Creating hard links to directories is not allowed.
  • 3. Hard links can only be created within the same file system.

Command parameters

Required parameters:

  • --backup[=CONTROL] Back up existing target files
  • -b similar--backup, but does not accept parameters
  • -d Allow the superuser to create hard links for directories
  • -f Force execution
  • -i Interactive mode; if the file exists, prompt the user whether to overwrite
  • -n Treat symbolic links as normal directories
  • -s Soft link (symbolic link)
  • -v Display detailed processing information

Optional 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

Examples

Create a symbolic link for a file. Create a symbolic link link2013 for the file log2013.log. 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 for a file. Create a hard link ln2013 for log2013.log. log2013.log and ln2013 have the same attributes.

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

Linux 命令大全Linux Command Reference

Other extensions