mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
commit
8e3d03138b
@ -1,121 +0,0 @@
|
||||
translated by cyleft
|
||||
|
||||
Linux ln Command Tutorial for Beginners (5 Examples)
|
||||
======
|
||||
|
||||
Sometimes, while working on the command line, you need to create links between files. This can be achieved using a dedicated command, dubbed **ln**. In this tutorial, we will discuss the basics of this tool using some easy to understand examples. But before we do that, it's worth mentioning that all examples here have been tested on an Ubuntu 16.04 machine.
|
||||
|
||||
### Linux ln command
|
||||
|
||||
As you'd have understood by now, the ln command lets you make links between files. Following is the syntax (or rather different syntax available) for this tool:
|
||||
|
||||
```
|
||||
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)
|
||||
```
|
||||
|
||||
And here's how the tool's man page explains it:
|
||||
```
|
||||
In the 1st form, create a link to TARGET with the name LINK_NAME. In the 2nd form, create a link
|
||||
to TARGET in the current directory. In the 3rd and 4th forms, create links to each TARGET in
|
||||
DIRECTORY. Create hard links by default, symbolic links with --symbolic. By default, each
|
||||
destination (name of new link) should not already exist. When creating hard links, each TARGET
|
||||
must exist. Symbolic links can hold arbitrary text; if later resolved, a relative link is
|
||||
interpreted in relation to its parent directory.
|
||||
```
|
||||
|
||||
The following Q&A-styled examples will give you a better idea on how the ln command works. But before that, it's good you get a understanding of what's the [difference between hard links and soft links][1].
|
||||
|
||||
### Q1. How to create a hard link using ln?
|
||||
|
||||
That's pretty straightforward - all you have to do is to use the ln command in the following way:
|
||||
|
||||
```
|
||||
ln [file] [hard-link-to-file]
|
||||
```
|
||||
|
||||
For example:
|
||||
|
||||
```
|
||||
ln test.txt test_hard_link.txt
|
||||
```
|
||||
|
||||
[![How to create a hard link using ln][2]][3]
|
||||
|
||||
So you can see a hard link was created with the name test_hard_link.txt.
|
||||
|
||||
### Q2. How to create soft/symbolic link using ln?
|
||||
|
||||
For this, use the -s command line option.
|
||||
|
||||
```
|
||||
ln -s [file] [soft-link-to-file]
|
||||
```
|
||||
|
||||
For example:
|
||||
|
||||
```
|
||||
ln -s test.txt test_soft_link.txt
|
||||
```
|
||||
|
||||
[![How to create soft/symbolic link using ln][4]][5]
|
||||
|
||||
The test_soft_link.txt file is a soft/symbolic link, as [confirmed][6] by its sky blue text color.
|
||||
|
||||
### Q3. How to make ln remove existing destination files of same name?
|
||||
|
||||
By default, ln won't let you create a link if a file of the same name already exists in the destination directory.
|
||||
|
||||
[![ln command example][7]][8]
|
||||
|
||||
However, if you want, you can make ln override this behavior by using the **-f** command line option.
|
||||
|
||||
[![How to make ln remove existing destination files of same name][9]][10]
|
||||
|
||||
**Note** : You can use the **-i** command line option if you want to make all this deletion process interactive.
|
||||
|
||||
### Q4. How to make ln create backup of existing files with same name?
|
||||
|
||||
If you don't want ln to delete existing files of same name, you can make it create backup of these files. This can be achieved using the **-b** command line option. Backup files created this way will contain a tilde (~) towards the end of their name.
|
||||
|
||||
[![How to make ln create backup of existing files with same name][11]][12]
|
||||
|
||||
A particular destination directory (other than the current one) can be specified using the **-t** command line option. For example:
|
||||
|
||||
```
|
||||
ls test* | xargs ln -s -t /home/himanshu/Desktop/
|
||||
```
|
||||
|
||||
The aforementioned command will create links to all test* files (present in the current directory) and put them in the Desktop directory.
|
||||
|
||||
### Conclusion
|
||||
|
||||
Agreed, **ln** isn't something that you'll require on daily basis, especially if you're a newbie. But it's a helpful command to know about, as you never know when it'd save your day. We've discussed some useful command line options the tool offers. Once you're done with these, you can learn more about ln by heading to its [man page][13].
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.howtoforge.com/linux-ln-command/
|
||||
|
||||
作者:[Himanshu Arora][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.howtoforge.com
|
||||
[1]:https://medium.com/meatandmachines/explaining-the-difference-between-hard-links-symbolic-links-using-bruce-lee-32828832e8d3
|
||||
[2]:https://www.howtoforge.com/images/command-tutorial/ln-hard-link.png
|
||||
[3]:https://www.howtoforge.com/images/command-tutorial/big/ln-hard-link.png
|
||||
[4]:https://www.howtoforge.com/images/command-tutorial/ln-soft-link.png
|
||||
[5]:https://www.howtoforge.com/images/command-tutorial/big/ln-soft-link.png
|
||||
[6]:https://askubuntu.com/questions/17299/what-do-the-different-colors-mean-in-ls
|
||||
[7]:https://www.howtoforge.com/images/command-tutorial/ln-file-exists.png
|
||||
[8]:https://www.howtoforge.com/images/command-tutorial/big/ln-file-exists.png
|
||||
[9]:https://www.howtoforge.com/images/command-tutorial/ln-f-option.png
|
||||
[10]:https://www.howtoforge.com/images/command-tutorial/big/ln-f-option.png
|
||||
[11]:https://www.howtoforge.com/images/command-tutorial/ln-b-option.png
|
||||
[12]:https://www.howtoforge.com/images/command-tutorial/big/ln-b-option.png
|
||||
[13]:https://linux.die.net/man/1/ln
|
@ -0,0 +1,115 @@
|
||||
为初学者准备的 Linux ln 命令教程(5 个示例)
|
||||
======
|
||||
|
||||
当我们在命令行上工作时,您可能需要创建文件链接。这时,您可以可以借助一个专用命令,**ln**。本教程中,我们将基于此命令通过一些简明的例子展开讨论。在此之前,有必要明确,本教程所有测试都是基于 Ubuntu 16.04 设备开展的。
|
||||
|
||||
### Linux ln 命令
|
||||
|
||||
正如现在你所了解的,ln 命令能够让您链接文件。下面就是 ln 工具的语法(或者使用其他一些可行的语法)。
|
||||
|
||||
```
|
||||
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)
|
||||
```
|
||||
|
||||
下面是 ln 工具 man 文档描述的内容:
|
||||
```
|
||||
在第一种形式下,创建名为 LINK_NAME 的链接目标。
|
||||
第二种形式为创建链接在当前目录。
|
||||
第三和第四中形式中,在 DIRECTORY 目录下创建链接目标。默认创建硬链接,字符链接需要 --symbolic 选项。默认创建硬链接,目标文件必须存在。字符链接可以保存任何文件。
|
||||
```
|
||||
|
||||
同故宫下面问答风格的例子,可能会给你更好的理解。但是在此之前,建议您先了解 [软连接和硬链接的区别][1].
|
||||
|
||||
### Q1. 如何通过 ln 命令创建硬链接?
|
||||
|
||||
这很简单,你只需要使用下面的 ln 命令:
|
||||
|
||||
```
|
||||
ln [file] [hard-link-to-file]
|
||||
```
|
||||
|
||||
这里有一个示例:
|
||||
|
||||
```
|
||||
ln test.txt test_hard_link.txt
|
||||
```
|
||||
|
||||
[![如何通过 ln 命令创建硬链接][2]][3]
|
||||
|
||||
如此,您便可以看见一个已经创建好了的硬链接,名为 test_hard_link.txt。
|
||||
|
||||
### Q2. 如何通过 ln 命令创建软/字符链接?
|
||||
|
||||
使用 -s 命令行选项
|
||||
|
||||
```
|
||||
ln -s [file] [soft-link-to-file]
|
||||
```
|
||||
|
||||
这里有一个示例:
|
||||
|
||||
```
|
||||
ln -s test.txt test_soft_link.txt
|
||||
```
|
||||
|
||||
[![如何通过 ln 命令创建软/字符链接][4]][5]
|
||||
|
||||
test_soft_link.txt 文件就是一个软/字符链接,被天蓝色文本 [标识][6]。
|
||||
|
||||
### Q3. 如何通过 ln 命令删除既存的同名目标文件?
|
||||
|
||||
默认情况下,ln 不允许您在目标文件目录下创建同名链接。
|
||||
|
||||
[![ln 命令示例][7]][8]
|
||||
|
||||
然而,如果一定要这么做,您可以使用 **-f** 命令行选项忽视此行为。
|
||||
|
||||
[![如何通过 ln 命令创建软/字符链接][9]][10]
|
||||
|
||||
**贴士** : 如果您想忽略删除过程中所有的命令行交互,您可以使用 **-i** 选项。
|
||||
|
||||
### Q4. 如何通过 ln 命令创建既存文件的同名备份?
|
||||
|
||||
如果您不想通过 ln 删除同名的既存文件,您可以让它为此文件创建备份。使用 **-b** 即可实现此效果。被创建的备份文件,会在其文件名结尾处包含一个(~) 字符标识。
|
||||
|
||||
[![如何通过 ln 命令创建既存文件的同名备份][11]][12]
|
||||
|
||||
使用 **-t** 选项指定一个文件目录(除了当前目录)。比如:
|
||||
|
||||
```
|
||||
ls test* | xargs ln -s -t /home/himanshu/Desktop/
|
||||
```
|
||||
|
||||
上述命令会为所有 test* 文件(当前目录下的 test* 文件)创建链接到桌面。
|
||||
|
||||
### 总结
|
||||
|
||||
当然,**ln** 并不是日常必备命令,尤其对于新手。但是了解此命令益处良多,有备无患,万一它哪一天刚好可以拯救你。对于这个命令,我们已经讨论了一些实用的选项,更多详情请查询 [man 文档][13]。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.howtoforge.com/linux-ln-command/
|
||||
|
||||
作者:[Himanshu Arora][a]
|
||||
译者:[CYLeft](https://github.com/CYLeft)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.howtoforge.com
|
||||
[1]:https://medium.com/meatandmachines/explaining-the-difference-between-hard-links-symbolic-links-using-bruce-lee-32828832e8d3
|
||||
[2]:https://www.howtoforge.com/images/command-tutorial/ln-hard-link.png
|
||||
[3]:https://www.howtoforge.com/images/command-tutorial/big/ln-hard-link.png
|
||||
[4]:https://www.howtoforge.com/images/command-tutorial/ln-soft-link.png
|
||||
[5]:https://www.howtoforge.com/images/command-tutorial/big/ln-soft-link.png
|
||||
[6]:https://askubuntu.com/questions/17299/what-do-the-different-colors-mean-in-ls
|
||||
[7]:https://www.howtoforge.com/images/command-tutorial/ln-file-exists.png
|
||||
[8]:https://www.howtoforge.com/images/command-tutorial/big/ln-file-exists.png
|
||||
[9]:https://www.howtoforge.com/images/command-tutorial/ln-f-option.png
|
||||
[10]:https://www.howtoforge.com/images/command-tutorial/big/ln-f-option.png
|
||||
[11]:https://www.howtoforge.com/images/command-tutorial/ln-b-option.png
|
||||
[12]:https://www.howtoforge.com/images/command-tutorial/big/ln-b-option.png
|
||||
[13]:https://linux.die.net/man/1/ln
|
Loading…
Reference in New Issue
Block a user