Use of Linux ln Command

  • 2021-08-28 21:36:13
  • OfStack

1. Brief introduction to commands

The ln command is used to create links for files, which are divided into hard links (hard link) and soft links (symbolic links, symbolic link). Hard links are created by default, and-s option is required to create soft links. This article describes the implementation of the GNU version, and other versions (such as the POSIX version) may have different implementations.

Note:

(1) Hard link is not a single file, just a filename. A file can have more than one file name, only the last file name from the disk to delete, to delete this file;
(2) Soft links can cross file systems, but hard links cannot cross file systems, because hard links are only aliases of files, not independent files;
(3) A hard link to a directory cannot be established because a hard link to a directory can cause the directory's inode to ring with the entity block. At this time, if the directory is deleted, the directory entity block will not be accessed by the system, resulting in an orphaned directory (which can no longer be accessed from the root directory);
(4) When creating a hard link, each target must exist. When creating a soft link, the target file may not exist;
(5) Soft link is an independent file containing path information, similar to the shortcut of Windows, and many of its attributes depend on the original file, so it is meaningless to set permissions for soft link files.

2. Command format


ln [OPTION]... [-T] TARGET LINK_NAME  (1st form)
ln [OPTION]... TARGET         (2nd form)
ln [OPTION]... TARGET... DIRECTORY   (3rd form)
ln [OPTION]... -t DIRECTORY TARGET... (4th form)

Format 1, which establishes a link with the specified name for the specified target file, is the longest used format;
The second format establishes a link with the same name in the current directory for the specified target file;
Format 3 and Format 4, respectively, establish a link with the same name for each target file in the specified directory.

3. Description of options

Mandatory parameters for long options are also mandatory for short options.


--backup[=CONTROL]
	 Back up every 1 Existing target files 
-b
	 Similar to  --backup But does not accept parameters 
-d, -F, --directory
	 Allow superusers to try to establish hard links to directories (Note that even superusers may fail due to system restrictions) 
-f,  - force
	 Forcibly establish a link to a file or directory, and the file or directory with the same name as the link will be overwritten 
-i,  - interactive
	 Ask the user before overwriting the existing file 
-L, --logical
	 When establishing a hard link, when the target file is a soft link, dereference is carried out to point to the target file of the soft link 
-n, --no-dereference
	 Think of soft links as 1 Like files, do not dereference 
-P, --physical
	 When creating a hard link, point directly to the soft link itself, not to the target file of the soft link (default) 
-r, --relative
	 Create a symbolic link relative to the link location 
-s, --symbolic
	 Establish a soft connection instead of a hard connection 
-S, --suffix=SUFFIX
	 Modify the backup file suffix. Use  -b  Parameter, the backup file suffix defaults to  ~
-t, --target-directory=DIRECTORY
	 Specify which directory to store the linked files in 
-T, --no-target-directory
	 Will  LINK_NAME  Treat as a linked file instead of a directory where the linked file is stored 
-v,  - verbose
	 Display instruction execution process 
--help
	 Show Help and Exit 
--version
	 Show version and exit 

Option-The parameter CONTROL of backup controls the version generation method after file backup, and the desirable values are as follows:


none, nil
	 Do not backup 
numbered, t
	 Scroll with numeric suffixes. Backup file name suffixes are incremented in sequence  ~1~ , 
existing, nil
	 If there is a numeric suffix, use numbers; Otherwise, use a simple backup method, that is, only backup 1 Times 
simple, never
	 Use only simple backup methods 

When using the option-s to generate soft links, the options-L and-P will be ignored, and a hard link will be established. By default, the option-P will be used to point the hard link to the soft link itself, which is equivalent to giving the soft link an alias.

4. Common examples

(1) Soft link to file/etc/passwd.


ln -s /etc/passwd passwdSoftLink

ll passwdSoftLink
lrwxrwxrwx 1 root root  11 Nov 13 22:21 passwdSoftLink -> /etc/passwd

(2) To file/etc/passwd multiple soft links, soft link name is the same, using numbers to represent the version number of the backup file. Multiple backups, the version number will be incremented in sequence.


ln -s --backup=numbered /etc/passwd passwdSoftLink

ll passwdSoftLink*
lrwxrwxrwx 1 root root  11 Nov 14 10:36 passwdSoftLink -> /etc/passwd
lrwxrwxrwx 1 root root  11 Nov 14 10:36 passwdSoftLink.~1~ -> /etc/passwd

(3) Establish soft links to non-existent files.


ln -s nofile nofileSoftLink

When you use the ll command to view a soft link, the name of the soft link is red, and the non-existent target file name is constantly flashing in white on a red background.

Writing content to the soft link nofileSoftLink and saving it will generate the file nofile.

(4) Establish a hard link to/etc/passwd.


ln /etc/passwd passwdHardLink

ll -i /etc/passwd passwdHardLink
787795 -rw-r--r-- 2 root root 1552 Jan 4 2019 /etc/passwd
787795 -rw-r--r-- 2 root root 1552 Jan 4 2019 passwdHardLink

When viewing two files using the ll command, column 1 has the same inode number, and column 3 has two hard links, indicating that there are two file names pointing to the data entity of the file.

(5) Create a soft link to/etc/passwd with the same name and place the soft link in the current directory. That is, use the 3rd and 4th command formats to link files.


ln -s /etc/passwd .

#  Or 
ln -s -t . /etc/passwd

#  View 
ll passwd
lrwxrwxrwx 1 root root  11 Nov 14 10:43 passwd -> /etc/passwd

Note that when writing the target file, the path should be relative to the target directory, or use an absolute path, otherwise the soft link cannot point to the target file.

(6) When the created linked file has a file with the same name, it is forced to overwrite without backup.


ln -sf /etc/passwd passwdSoftLink

(7) Modify the soft link to point to the new target file. Point the soft link passwdSoftLink to/usr/bin/passwd, re-establish the soft link, and forcibly overwrite the original soft link passwdSoftLink.


ln -sf /usr/bin/passwd passwdSoftLink

ll passwdSoftLink
lrwxrwxrwx 1 root root 15 Nov 14 10:52 passwdSoftLink -> /usr/bin/passwd

The above is the use of Linux ln command details, more about Linux ln command information please pay attention to other related articles on this site!


Related articles: