Linux: ln command

About ln

ln creates links between files.


Syntax

ln [OPTION]... TARGET [...] [LINK_NAME [...]]

Description

ln creates a link to file TARGET with the name LINK_NAME. If LINK_NAME is omitted, a link to TARGET is created in the current directory, using the name of TARGET as the LINK_NAME.
ln creates hard links by default, or symbolic links if the -s (–symbolic) option is specified. When creating hard links, each TARGET must exist.


Options

--backup[=CONTROL]  make a backup of each existing destination file.
-b          like --backup, but does not accept an argument.
-d, -F, --directory allow the superuser to attempt to hard link directories (although it will probably fail due to system restrictions, even for the superuser).
-f, --force     remove existing destination files.
-i, --interactive   prompt whether to remove destinations.
-L, --logical       dereference TARGETs that are symbolic links. In other words, treat the symbolic link target as a file, not a link to a target.
-n, --no-dereference    treat LINK_NAME as a normal file if it is a symbolic link to a directory.
-P, --physical      make hard links directly to symbolic links.
-r, --relative      create symbolic links relative to link location.
-s, --symbolic      make symbolic links instead of hard links.
-S, --suffix=SUFFIX override the usual backup suffix.
-t, --target-directory=DIRECTORY    specify the DIRECTORY in which to create the links.
-T, --no-target-directory   always treat LINK_NAME as a normal file.
-v, --verbose       print name of each linked file.
--help          display a help message and exit.
--version       output version information and exit.

Examples

$ ls -l
total 4
lrwxrwxrwx 1 chris chris 10 2010-09-17 23:40 file1 -> sample.txt
-rw-r--r-- 1 chris chris 22 2010-09-17 23:36 sample.txt

The 1st character in each and every line of the ls command output indicates one of the following file types. If the 1st character is l (lower case L), then it is a link file.

- regular file
l link file
d directory
p pipe
c character special device
b block special device

Soft Link
Linux OS recognizes the data part of this special file as a reference to another file path. The data in the original file can be accessed through the special file, which is called as Soft Link.

To create a soft link, do the following (ln command with -s option):

$ ln -s /full/path/of/original/file /full/path/of/soft/link/file

Hard Link
With Hard Link, more than one file name reference the same inode number. Once you create a directory, you would see the hidden directories “.” and “..” . In this, “.” directory is hard linked to the current directory and the “..” is hard linked to the parent directory.

When you use link files, it helps us to reduce the disk space by having single copy of the original file and ease the administration tasks as the modification in original file reflects in other places.

To create a hard link, do the following (ln command with no option):

$ ln /full/path/of/original/file /full/path/of/hard/link/file

Create a symbolic link for a File
The following examples creates a symbolic link library.so under /home/chris/lib, based on the library.so located under /home/chris/src/ directory.

$ cd /home/chris/lib 
$ ln -s /home/chris/src/library.so library.so
$ ls -l library.so
lrwxrwxrwx  1 chris chris       21 2010-09-18 07:23 library.so -> /home/chris/src/library.so

Create a symbolic link for a Directory
Just like file, you can create symbolic link for directories as shown below.

$ mkdir /home/chris/obj
$ cd tmp
$ ln -s /home/chris/obj objects
$ ls -l objects
lrwxrwxrwx 1 chris chris       6 2010-09-19 16:48 objects -> /home/chris/obj

Note: The inode of the original file/directory and the soft link should not be identical.


The inode number for the hard linked files would be same. The hard link for files can be created as follows,

$ ln src_original.txt dst_link.txt
$ ls -i dst_link.txt
253564 dst_link.txt
$ ls -i src_original.txt
253564 src_original.txt

Note: Unix / Linux will not allow any user (even root) to create hard link for a directory.


When you want to create the link across partitions, you are allowed to create only the symbolic links. Creating hard link across partitions is not allowed, as Unix can’t create/maintain same inode numbers across partitions.

You would see the “Invalid cross-device link” error when you are trying to create a hard link file across partitions.

# mount /dev/sda5 /mnt
# cd /mnt
# ls
main.c Makefile
# ln Makefile /tmp/Makefile
ln: creating hard link `/tmp/Makefile' to `Makefile': Invalid cross-device link

And the symbolic link can be created in the same way as we did in the above.


Backup the Target Files If it Already Exists

When you create a new link (if another file exist already with the same name as the new link name), you can instruct ln command to take a backup of the original file before creating the new link using the –backup option as shown below.

$ ls
ex1.c  ex2.c
$ ln --backup -s ex1.c ex2.c 
$ ls -lrt
total 8
-rw-r--r-- 1 chris chris 20 2010-09-19 16:57 ex1.c
-rw-r--r-- 1 chris chris 20 2010-09-19 16:57 ex2.c~
lrwxrwxrwx 1 chris chris  5 2010-09-19 17:02 ex2.c -> ex1.c

Note: If you don’t want the backup and overwrite the existing file then use -f option.


While creating a new soft link, normally OS would de-reference the destination path before it creates the new soft link.

Sometimes you might not want ln command to create the new link, if the destination path is already a symbolic link that is pointing to a directory.

Following examples shows a normal way of creating soft link inside a directory.

$ cd ~
$ mkdir example
$ ln -s /etc/passwd example
$ cd example/
$ ls -l
total 0
lrwxrwxrwx 1 root root 16 2010-09-19 17:24 passwd -> /etc/passwd

In case the “example” directory in the above code-snippet is a symbolic link pointing to some other directory (for example second-dir), the ln command shown will still create the link under second-dir. If you don’t want that to happen, use ln -n option as shown below.

$ cd ~
$ rm -rf example
$ mkdir second-dir
$ ln -s second-dir example
$ ln -n -s /etc/passwd example
ln: creating symbolic link `example': File exists

Note: In the above example, if you don’t use the -n option, the link will be created under ~/second-dir directory.


In the following example, there are two directories — first-dir and second-dir. The directory first-dir contains couple of C program files. If you want to create soft links for these files in second-dir, you’ll typically do it one by one. Instead, you can create soft list for multiple files together using -t option as shown below.

$ ls
first-dir second-dir
$ ls first-dir
ex1.c  ex2.c
$ cd second-dir
$ ln -s ../first-dir/*.c -t .
$ ls -l
total 0
lrwxrwxrwx 1 chris chris 14 2010-09-19 15:20 ex1.c -> ../first-dir/ex1.c
lrwxrwxrwx 1 chris chris 14 2010-09-19 15:20 ex2.c -> ../first-dir/ex2.c

Keep in mind that whenever you are creating link files with -t option, it is better to go into target directory and perform the link creation process. Otherwise, you would face the broken link files as shown below.

$ cd first-dir
$ ln -s *.c /home/chris/second-dir
$ cd /home/chris/second-dir
$ ls -l
total 0
lrwxrwxrwx 1 chris chris 5 2010-09-19 15:26 ex1.c -> ex1.c
lrwxrwxrwx 1 chris chris 5 2010-09-19 15:26 ex2.c -> ex2.c

Instead, you might also use actual path for source files to create the link properly.


When the original file referred by a soft-link is deleted, the soft link will be broken as shown below.

$ ln -s file.txt /tmp/link
$ ls -l /tmp/link
lrwxrwxrwx 1 chris chris 9 2010-09-19 15:38 /tmp/link -> file1.txt
$ rm file.txt
$ ls -l /tmp/link
lrwxrwxrwx 1 chris chris 9 2010-09-19 15:38 /tmp/link -> file1.txt

Let us assume that you have two partitions – 5GB and 20GB. The first partition does not have too much free space available in it. If a program located on the first partition needs more space (For example, for it’s log file), you can use some of the space from the second partition by creating a link for the log files as shown below.

Consider that partition1 is mounted on /, and partition2 is mounted to /mnt/. Let us assume that the logs that are located on partition1 is running out of space, and you’ve decided to move them to partition2. You can achieve this as shown below.

$ mkdir /mnt/logs
$ cd /logs
$ mv * /mnt/logs
$ cd /; rmdir logs
$ ln -s /mnt/logs logs

Removing the Hard Linked Files

When you delete a file that is hard linked, you would be still able to access the content of the file until you have the last file which is hard linked to it, as shown in the example below.

Create a sample file.

$ vim src_original.txt
Created this file to test the hard link.

Create a hard link to the sample file.

$ ln src_original.txt dst_link.txt

Delete the original file.

$ rm src_original.txt

You can still access the original file content by using the hard link you created.

$ cat dst_link.txt
Created this file to test the hard link.

Reference

http://www.thegeekstuff.com/2010/10/linux-ln-command-examples/
http://www.computerhope.com/unix/uln.htm

<think>好的,用户遇到了在Linux系统中找不到rar命令的问题,需要安装。首先,我得回忆一下相关的知识。根据之前的引用,用户已经安装了rarlinux的压缩包,但可能没有正确配置路径或者安装步骤有遗漏。 首先,用户可能下载了正确的安装包,比如rarlinux-x64-6.0.1.tar.gz,但安装步骤可能没有完成后续的配置。根据引用4和5,安装步骤包括解压、编译安装,然后将rar和unrar命令链接到系统路径中,比如/usr/local/bin。用户可能只解压了文件,但没有执行make install或者创建符号链接,导致系统找不到命令。 接下来,我需要按照步骤详细说明如何正确安装。首先,下载正确的安装包,根据系统是32位还是64位选择。然后解压,进入目录,执行make install。这里可能需要root权限,所以要用sudo。安装完成后,rar和unrar通常会被放在/usr/local/rar目录下,这时候需要创建软链接到/usr/local/bin,这样系统就能找到命令了。 另外,用户可能没有将安装目录添加到PATH环境变量中,所以即使安装了,系统也无法识别。因此,创建符号链接到系统路径是一个关键步骤,如引用4所述。如果用户按照这些步骤操作,应该就能解决问题了。 最后,验证安装是否成功,可以运行rar --version或者尝试解压一个.rar文件。如果还是不行,可能需要检查权限问题,或者是否确实安装了正确的版本。还可能存在依赖问题,但根据引用中的信息,RAR for Linux通常是自包含的,不需要额外依赖。 总结下来,正确的安装步骤包括下载、解压、编译安装、创建符号链接,然后验证。确保用户每一步都正确执行,尤其是权限和路径的设置。</think>### 如何在Linux上安装rar命令? #### 步骤说明 1. **下载RAR安装包** 根据系统架构选择对应版本(32位或64位): ```bash # 64位系统 wget https://www.rarlab.com/rar/rarlinux-x64-6.0.1.tar.gz # 32位系统 wget https://www.rarlab.com/rar/rarlinux-5.9.1.tar.gz ``` 2. **解压并安装** 解压后进入目录执行安装命令: ```bash tar -xzvf rarlinux-*.tar.gz cd rar sudo make install ``` 3. **配置环境变量** 创建软链接到系统命令目录(确保`/usr/local/bin`在`PATH`中): ```bash sudo ln -s /usr/local/rar/rar /usr/local/bin/rar sudo ln -s /usr/local/rar/unrar /usr/local/bin/unrar ``` 4. **验证安装** 检查是否成功: ```bash rar --version unrar --version ``` #### 补充说明 - 若安装后仍提示命令未找到,检查符号链接路径是否正确[^4]。 - 部分发行版可通过包管理器安装(如Ubuntu/Debian): ```bash sudo apt install rar unrar ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值