|
ln 命令用来在文件间创建连接,可以是硬连接,也可以是软连接,默认情况下创建硬连接。
1. 建立硬连接$ ln target target_hdlink 其中,target 是一个已存在的的目标文件,后面的 target_hdlink 是要建立的硬连接。
2. 使用 -s 参数建立软连接$ ln -s target target_stlink
$ ls -al target_stlink
lrwxrwxrwx 1 beyes beyes 6 Jun 23 07:42 target_stlink -> target
3. 使用 -i, --interactive 选项交互式建立硬连接
比如,在上面已经对 target 文件建立了一个硬连接为 target_hdlink ,如果再次按照简单的方式建立一个同名的硬连接将不会被允许:$ ln -s target target_stlink
ln: creating symbolic link `target_stlink': File exists 此时可以采用 -i 选项进行建立之前的问询是否要覆盖:$ ln -i target target_hdlink
ln: replace `target_hdlink'? y 如果输入 y 并回车表示覆盖。
4. 使用 -f, --force 选项表示强制性覆盖:$ ln -f target target_hdlink
5. 使用 -b 或 --backup 选项进行先备份再覆盖:$ ln -b target target_hdlink
$ ls
target target_hdlink target_hdlink~ 备份文件后面有一个波浪号 '~' 。
-b 选项后面不接参数,所以给原来的硬连接生成的备份文件只是在它原来的名字后面添加一个波浪号;如果希望备份到另外的文件名,可以使用 --backup 选项来指定,--backup 选项后面接控制参数,这些控制参数会按照它们的规则命名备份文件,比如:$ ln --backup=numbered target target_hdlink
$ ln --backup=numbered target target_hdlink
$ ln --backup=t target target_hdlink
$ ls
target target_hdlink~ target_stlink
target_hdlink target_hdlink.~1~ target_hdlink.~2~ target_hdlink.~3~ 上面使用参数 numbered 控制产生的备份名用数字来标识,连续使用则数字递增。其它的控制选项还有: - `none', `off'
- `simple', `never'
- `existing', `nil'
- `numbered', `t' 上面的控制参数中,每行中的第 2 个是第 1 个参数的缩写。可以用 ln --help 命令看更详细的帮助信息。
6. 使用 -S, --suffix 选项自定义备份后缀
如果希望自定义自己的备份后缀,那么使用 -S, --suffix 选项:$ ln -b -S .hdback target target_hdlink
$ ls
target target_hdlink target_hdlink.hdback target_stlink
7. 为多个文件和目录建立软连接
在 man 手册里对 ln 的使用列出了几种模式,其中第 3 种是:ln [OPTION]... TARGET... DIRECTORY (3rd form) 它的意思是,在 DIRECTORY 这个目录里为多个文件和目录(都是 TARGET)建立连接。下面假定一个目录下有这些文件:$ ls -al
total 16
drwxr-xr-x 4 beyes beyes 4096 Jun 23 08:27 .
drwxr-xr-x 3 beyes beyes 4096 Jun 23 06:50 ..
-rw-r--r-- 1 beyes beyes 0 Jun 23 08:23 file1
-rw-r--r-- 1 beyes beyes 0 Jun 23 08:23 file2
-rw-r--r-- 1 beyes beyes 0 Jun 23 08:25 file3
drwxr-xr-x 2 beyes beyes 4096 Jun 23 08:26 filedir
drwxr-xr-x 2 beyes beyes 4096 Jun 23 08:27 SLDir 现在打算在 SLDir 里建立 file1, file2, file3 以及 filedir 这个目录的软连接,可以这么使用:$ ln -s /home/beyes/command/ln/file1 /home/beyes/command/ln/file2 /home/beyes/command/ln/file3 /home/beyes/command/ln/filedir/ SLDir/ 查看建立是否成功:$ ls -al
total 8
drwxr-xr-x 2 beyes beyes 4096 Jun 23 08:27 .
drwxr-xr-x 4 beyes beyes 4096 Jun 23 08:27 ..
lrwxrwxrwx 1 beyes beyes 28 Jun 23 08:27 file1 -> /home/beyes/command/ln/file1
lrwxrwxrwx 1 beyes beyes 28 Jun 23 08:27 file2 -> /home/beyes/command/ln/file2
lrwxrwxrwx 1 beyes beyes 28 Jun 23 08:27 file3 -> /home/beyes/command/ln/file3
lrwxrwxrwx 1 beyes beyes 31 Jun 23 08:27 filedir -> /home/beyes/command/ln/filedir/ 这里需要注意的是,SLDir 目录是必须事先创建的,不能凭空指定一个目录。另外,因为这些软连接是在 SLDir 目录中创建的,所以这里要用绝对路径,这样就可以避免相对路径带来的访问错误。比如,使用 ./file1 这种方式创建的符号连接,这样会因为在 SLDir 里没有 file1 文件可以连接而产生找不到指向文件的错误。这只是建立软连接的要求,如果是建立硬连接因为文件的 inode 是相同的,故不存在这样的问题。
8. 为一个目录建立硬连接
为一个目录建立硬连接需要用 -d 选项,命令格式是:这种做法需要用 root 用户,否则会失败;但在一些系统上,连 root 用户也是不允许的。
9. -n, --no-dereference 选项
-n 选项将一个目录的软连接视为普通的文件。比较有 -n 选项和没有 -n 选项的两种情况,下面分别对一个目录用有 -n 选项和无 -n 选项来建立它的软连接:$ ln -s Dir1 Dir1SL
$ ln -s Dir1 Dir1SLF
beyes@debian:~/command/ln$ ls -al
total 12
drwxr-xr-x 3 beyes beyes 4096 Jun 23 09:23 .
drwxr-xr-x 3 beyes beyes 4096 Jun 23 06:50 ..
drwxr-xr-x 2 beyes beyes 4096 Jun 23 09:24 Dir1
lrwxrwxrwx 1 beyes beyes 4 Jun 23 09:22 Dir1SL -> Dir1
lrwxrwxrwx 1 beyes beyes 4 Jun 23 09:23 Dir1SLF -> Dir1 从 ls -al 命令的输出来看,他们并没有什么区别。但要注意,同名的软连接是可以直接覆盖而无需询问的,如再执行一次 ln -s Dir1 Dir1SL 命令将不会有任何提示,但是再建立一个 Dir1SLF 时就会出现问题(因为-n 选项将它视为普通文件,而普通文件如果存在的话,是会有提示的):$ ln -s Dir1 Dir1SLF
ln: creating symbolic link `Dir1SLF/Dir1': File exists -n 选项的一个目的是,防止很轻易的就将一个同名软连接覆盖原有的软连接。 |
|