In linux ln command use parameters detailed

Source: Internet
Author: User
Tags syslog centos

LN is another very important command in Linux, its function is to create a link for a file in another location, the most common parameter of this command is-s, which is the Ln–s source file destination file.
When we need to use the same file in a different directory, we don't need to put a file that must be the same in every directory we need, we just put the file in a certain directory, and then use the LN command link in the other directory to make it available, without duplicating the disk space. For example: ln–s/bin/less/usr/local/bin/less
-S is the meaning of the Code name (symbolic).
Here are two points to note: first, the LN command keeps the synchronization of each linked file, which means that no matter which one you change, the other files will change the same; second, the links in Ln are soft links and hard links, and soft links are ln–s * * * *, It will only generate a mirror image of the file in your chosen location, does not consume disk space, hard link LN * * *, no parameter-s, it will generate a file of the same size as the source file in your chosen position, whether it is a soft link or a hard link, the files are kept synchronized.
If you use LS to look at a directory, found that there is a file behind a @ symbol, that is a file in the ln command generated, with the ls–l command to view, you can see the path of link displayed.
Instructions detailed instructions
Directive Name: LN
Use Rights: All users
How to use: ln [options] Source dist, where option is in the form of:
[-BDFINSVF] [-S Backup-suffix] [-v {numbered,existing,simple}]
[--help] [--version] [--]
Description: Linux/unix file system, there is a so-called link, we can treat it as a file alias, and the link can be divided into two kinds: hard link (hard link) and soft link (symbolic link), hard link means that a file can have more than one name, The soft link is to create a special file that points to the location of another file. Hard links exist in the same file system, while soft links can span different file systems.
The LN source Dist produces a link (dist) to source, as with a hard link or a soft link is determined by the parameter.
Either a hard link or a soft link will not copy the original file, it will only occupy a very small amount of disk space.
-F: First delete files with dist file name when linking
-D: Allows system managers to hard link their own directories
-I: Ask before deleting files with the same file name as Dist
-N: Dist is considered a general file when soft links are made
-S: Soft link (symbolic link)
-V: Displays the file name before the link
-B: Backup files that will be overwritten or deleted when the link is made
-S SUFFIX: Add the backup file to the end of the SUFFIX
-V Method: Specify how the backup should be
--HELP: Show Auxiliary Instructions
--version: Display version


Example:

1. Create a connection to a file

[Root@a ~]# ln-s/home/kk/ss.sh ~ #如果不写目标地址, that is, in the current directory built link

[Root@a ~]# ls

Centos-base.repo.oldboy anaconda-ks.cfg install.log install.log.syslog ss.sh This command indicates that a link to the source file with the same name is created in the home directory to/home/kk/ss.sh Symbolic connection

[Root@a ~]# ln-s/home/kk/ss.sh ~/dd

[Root@a ~]# ls

Centos-base.repo.oldboy anaconda-ks.cfg DD Install.log install.log.syslog

This command indicates that a symbolic connection is established in the home directory that links to/home/kk/ss.sh and the name is DD (the premise is that there is no ABC this directory in the home directory)

2. Create a symbolic connection to a directory

[Root@a home]# ln-s/HOME/KK/HOME/ABC

[Root@a home]# ls

ABC Apache KK Oldboy

[Root@a home]# CD ABC

[Root@a abc]# ls

ss.sh

This command represents creating a symbolic connection in the/home directory that is linked to the/HOME/KK directory with the name ABC.

[Root@a home]# RM ABC

Rm:remove Symbolic link ' abc '? Y #删除所作目录链接时的提示

[Root@a ~]# ln-s/tmp ~/

[Root@a ~]# ls

Centos-base.repo.oldboy anaconda-ks.cfg Install.log install.log.syslog tmp

This command indicates that a symbolic connection with the same name as the source folder is created in the home directory

3. Create a hard connection to a file

[Root@a ~]# ln/home/kk/ss.sh ~/

[Root@a ~]# Ls-l

Total 60

-rw-r--r--1 root root 2239 Oct 09:30 centos-base.repo.oldboy

-RW-------1 root 1074 Nov 12:28 anaconda-ks.cfg

-rw-r--r--1 root root 26403 Nov 12:28 Install.log

-rw-r--r--1 root root 4378 Nov 12:28 install.log.syslog

-rw-r--r--2 root 14:31 Dec ss.sh

This command indicates that a link to the/home/kk/ss.sh hard link is created in the home directory with the same name as the source file, and the destination file is the same color as the original.

Ls–l also cannot show the path of the link

[Root@a ~]# ln/home/kk/ss.sh ~/ABC

[Root@a ~]# ls

Centos-base.repo.oldboy ABC anaconda-ks.cfg Install.log install.log.syslog

This command indicates the establishment of a link to/tmp/abc.txt in the home directory and a hard link with the name ABC (provided that there is no ABC of this directory in the home directory)

The existence of the ABC catalogue

[Root@a ~]# mkdir ABC

[Root@a ~]# ls

Centos-base.repo.oldboy ABC anaconda-ks.cfg Install.log install.log.syslog

[Root@a ~]# ln/home/kk/ss.sh ~/abc #做硬链接

[Root@a ~]# ls

Centos-base.repo.oldboy ABC anaconda-ks.cfg Install.log install.log.syslog

[Root@a ~]# CD ABC

[Root@a abc]# ls

ss.sh

Source file is directory can not do hard link

[Root@a ~]# Ln/home/kk ~/ABC

ln: '/home/kk ': Hard link not allowed for directory

4. Parameter-V Usage

Hard Links:

[Root@a home]# ln-v/home/kk/ss.sh/home #加上参数v后,

Create hard link '/home/ss.sh ' to '/home/kk/ss.sh ' #会用文字显示源文件到目标的链接

[Root@a home]# ls

Apache KK Oldboy ss.sh

[Root@a home]# Ln-vs/home/kk/ss.sh/home #创建名子相同的软链接

Create symbolic link '/home/ss.sh ' to '/home/kk/ss.sh ' #会用文字显示源文件到目标的链接

ln:creating Symbolic link '/home/ss.sh ' to '/home/kk/ss.sh ': File exists #同时也出现了己存在提示

Soft links

[Root@a home]# Ln-vs/home/kk/ss.sh/home/dd #加上参数v后的软链接

Create symbolic link '/home/dd ' to '/home/kk/ss.sh ' #会用文字显示源文件到目标的链接

[Root@a home]# ls

Apache DD KK Oldboy ss.sh

Green soft links, the same as the original color is a hard link

The use of 5.-f

[Root@a home]# ls

Apache DD KK Oldboy ss.sh #dd is a soft link exists; ss.sh is a hard link.

[Root@a home]# ln-s-f/home/kk/ss.sh/home #创建/home/kk/ss.sh Soft Links

[Root@a home]# ls

Apache DD KK Oldboy ss.sh #上面加了f参数, ss.sh is now a soft link

[Root@a home]# ln/home/kk/ss.sh/home/dd #创建/home/kk/ss.sh hard link, the name is also DD

ln:creating hard link '/home/dd ' to '/home/kk/ss.sh ': File exists #提示出现了

[Root@a home]# ln-f/home/kk/ss.sh/home/dd #加上f参数后, no hint.

[Root@a home]# ls

Apache DD KK Oldboy ss.sh #dd变成链接了,

The main process of-F: First delete the soft link dd with the same name, and then create the hard link dd

Usage of 6.-d and b

[Root@a home]# Ln/home/kk/home/dd #创建硬链接不允许

ln: '/home/kk ': Hard link not allowed for directory #不允许对目录创建硬链接

[Root@a home]# ln-f/home/kk/home/dd #加上-D parameter

ln:creating hard link '/home/dd ' to '/home/kk ': Operation not permitted #为什么还是不行呀?

The use of 7.-i

[Root@a home]# ln-s/home/kk/ss.sh #创建软链接

[Root@a home]# ls #查看

Apache KK Oldboy ss.sh

[Root@a home]# ln-fi/home/kk/ss.sh #加上-F parameter deletes the same name plus-I argument-prompts before deleting

Ln:replace './ss.sh '? #提示

[Root@a home]# ln-fi/home/kk/ss.sh

Ln:replace './ss.sh '? N #我选择了n

[Root@a home]# ls #查看

Apache KK Oldboy ss.sh #还存在

[Root@a home]# ln-fb/home/kk/ss.sh #加上参数b

[Root@a home]# ls #查看

Apache KK Oldboy ss.sh ss.sh~ #之前存在的软链接被更名备份了, and then create a hard link with the same name


Attention:

First, the LN command keeps the synchronization of each linked file, which means that no matter which one you change, the other files will change the same.

Second, the links in Ln are soft links and hard links two kinds, soft link is ln-s * * * *, it will only in the location you selected to generate a file image, does not occupy disk space, hard link LN * * * *, no parameter-s, it will be in the location you selected to generate a file and the same size as the source file, The files remain synchronized, whether they are soft links or hard links. Soft links can span partitions, but hard links can only be within the same partition.

Third: directory cannot establish hard link, but can establish soft link.

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.