Merge remote-tracking branch 'LCTT/master'

This commit is contained in:
wxy 2018-02-05 23:06:15 +08:00
commit b7db4bfbfc
5 changed files with 219 additions and 224 deletions

View File

@ -0,0 +1,103 @@
为什么应该在 Linux 上使用命名管道
======
> 命名管道并不常用,但是它们为进程间通讯提供了一些有趣的特性。
![](https://images.techhive.com/images/article/2017/05/blue-1845806_1280-100722976-large.jpg)
估计每一位 Linux 使用者都熟悉使用 “|” 符号将数据从一个进程传输到另一个进程的操作。它使用户能简便地从一个命令输出数据到另一个命令,并筛选出想要的数据而无须写脚本进行选择、重新格式化等操作。
还有另一种管道, 虽然也叫“管道”这个名字却有着非常不同的性质。即您可能尚未使用甚至尚未知晓的——命名管道。
普通管道与命名管道的一个主要区别就是命名管道是以文件形式实实在在地存在于文件系统中的,没错,它们表现出来就是文件。但是与其它文件不同的是,命名管道文件似乎从来没有文件内容。即使用户往命名管道中写入大量数据,该文件看起来还是空的。
### 如何在 Linux 上创建命名管道
在我们研究这些空空如也的命名管道之前,先追根溯源来看看命名管道是如何被创建的。您应该使用名为 `mkfifo` 的命令来创建它们。为什么提及“FIFO”是因为命名管道也被认为是一种 FIFO 特殊文件。术语 “FIFO” 指的是它的<ruby>先进先出<rt>first-in, first-out</rt></ruby>特性。如果你将冰淇淋盛放到碟子中然后可以品尝它那么你执行的就是一个LIFO<ruby>后进先出<rt>last-in, first-out</rt></ruby>操作。如果你通过吸管喝奶昔,那你就在执行一个 FIFO 操作。好,接下来是一个创建命名管道的例子。
```
$ mkfifo mypipe
$ ls -l mypipe
prw-r-----. 1 shs staff 0 Jan 31 13:59 mypipe
```
注意一下特殊的文件类型标记 “p” 以及该文件大小为 0。您可以将重定向数据写入命名管道文件而文件大小依然为 0。
```
$ echo "Can you read this?" > mypipe
```
正如上面所说敲击回车后似乎什么都没有发生LCTT 译注:没有返回命令行提示符)。
另外再开一个终端,查看该命名管道的大小,依旧是 0
```
$ ls -l mypipe
prw-r-----. 1 shs staff 0 Jan 31 13:59 mypipe
```
也许这有违直觉,用户输入的文本已经进入该命名管道,而你仍然卡在输入端。你或者其他人应该等在输出端,并准备读取放入管道的数据。现在让我们读取看看。
```
$ cat mypipe
Can you read this?
```
一旦被读取之后,管道中的内容就没有了。
另一种研究命名管道如何工作的方式是通过将放入数据的操作置入后台来执行两个操作(将数据放入管道,而在另外一段读取它)。
```
$ echo "Can you read this?" > mypipe &
[1] 79302
$ cat mypipe
Can you read this?
[1]+ Done echo "Can you read this?" > mypipe
```
一旦管道被读取或“耗干”,该管道就清空了,尽管我们还能看见它并再次使用。可为什么要费此周折呢?
### 为何要使用命名管道?
命名管道很少被使用的理由似乎很充分。毕竟在 Unix 系统上,总有多种不同的方式完成同样的操作。有多种方式写文件、读文件、清空文件,尽管命名管道比它们来得更高效。
值得注意的是,命名管道的内容驻留在内存中而不是被写到硬盘上。数据内容只有在输入输出端都打开时才会传送。用户可以在管道的输出端打开之前向管道多次写入。通过使用命名管道,用户可以创建一个进程写入管道并且另外一个进程读取管道的流程,而不用关心协调二者时间上的同步。
用户可以创建一个单纯等待数据出现在管道输出端的进程,并在拿到输出数据后对其进行操作。下列命令我们采用 `tail` 来等待数据出现。
```
$ tail -f mypipe
```
一旦供给管道数据的进程结束了,我们就可以看到一些输出。
```
$ tail -f mypipe
Uranus replicated to WCDC7
Saturn replicated to WCDC8
Pluto replicated to WCDC9
Server replication operation completed
```
如果研究一下向命名管道写入的进程,用户也许会惊讶于它的资源消耗之少。在下面的 `ps` 命令输出中唯一显著的资源消耗是虚拟内存VSZ 那一列)。
```
ps u -P 80038
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
shs 80038 0.0 0.0 108488 764 pts/4 S 15:25 0:00 -bash
```
命名管道与 Unix/Linux 系统上更常用的管道相比足以不同到拥有另一个名号,但是“管道”确实能反映出它们如何在进程间传送数据的形象,故将称其为“命名管道”还真是恰如其分。也许您在执行操作时就能从这个聪明的 Unix/Linux 特性中获益匪浅呢。
--------------------------------------------------------------------------------
via: https://www.networkworld.com/article/3251853/linux/why-use-named-pipes-on-linux.html
作者:[Sandra Henry-Stocker][a]
译者:[YPBlib](https://github.com/YPBlib)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.networkworld.com/author/Sandra-Henry_Stocker/
[1]:http://www.networkworld.com/article/2926630/linux/11-pointless-but-awesome-linux-terminal-tricks.html#tk.nww-fsb

View File

@ -1,3 +1,4 @@
##Name1e5s Translating##
Open source software: 20 years and counting
============================================================

View File

@ -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

View File

@ -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

View File

@ -1,103 +0,0 @@
为什么应该在Linux上使用命名管道
======
![](https://images.techhive.com/images/article/2017/05/blue-1845806_1280-100722976-large.jpg)
估计每一位Linux使用者都熟悉使用 “|”符号将数据从一个进程传输到另一个进程的操作。它使用户能简便地从一个命令输出数据到另一个命令并筛选出想要的数据而无须写脚本进行选择、重定格式等操作。
还有另一种管道, 虽然也叫“管道”这个名字却有着非常不同的性质。即您可能尚未使用甚至尚未知晓的——命名管道。
**另可参考:[11 pointless but awesome Linux terminal tricks][1]**
普通管道与命名管道的一个主要区别就是命名管道是它们以文件形式实实在在地存在于系统中存在。但是与其它文件不同的是,命名管道文件似乎从来没有文件内容。即使用户往命名管道中写入大量数据,该文件看起来还是空的。
### 如何在Linux上创建命名管道
Before在我们研究这些空命名管道之前先追根溯源来看看命名管道是如何被创建的。您可以使用**mkfifo** 命令。提到“FIFO”是因为命名管道也被认为是FIFO特殊文件。术语“FIFO”指的是它的先进先出("first-in, first-out ")属性。如果某人在吃一个甜筒冰淇淋那么他执行的就是一个LIFO(后进先出last-in, first-out)操作。如果他通过吸管饮食一个冰淇淋那就在执行一个FIFO操作。好接下来是一个创建命名管道的例子。
```
$ mkfifo mypipe
$ ls -l mypipe
prw-r-----. 1 shs staff 0 Jan 31 13:59 mypipe
```
(译者注:实际环境中当您输入`mkfifo mypipe`后命令行可能会阻塞在当前位置这是因为以只写方式打开的FIFO要阻塞到其他某个进程以读方式打开该FIFO。您可在另一个终端中输入 `cat < mypipe`解决注意两个终端中保证mypipe的路径一致。注意一下特殊的文件类型标记“p”以及文件大小为0。您可以将重定向数据写入命名管道文件而文件大小依然为0。
```
$ echo "Can you read this?" > mypipe
$ ls -l mypipe
prw-r-----. 1 shs staff 0 Jan 31 13:59 mypipe
```
正如上面所说,敲击回车似乎什么都没有发生。
```
$ echo "Can you read this?" > mypipe
```
不那么显然的是,用户输入的文本已经进入该命名管道。可能用户却还在盯着输入端,事实上应有用户或其他人在输出端读取接受数据。
```
$ cat mypipe
Can you read this?
```
一旦被读取之后,管道中的内容就不再被保存。
另一种研究命名管道如何工作的方式是通过在后台传送数据的方式完成传送和接受两样操作。
```
$ echo "Can you read this?" > mypipe &
[1] 79302
$ cat mypipe
Can you read this?
[1]+ Done echo "Can you read this?" > mypipe
```
一旦管道被读取或“耗干”,该管道就清空了,尽管我们还能看见它并再次使用,可为什么要费此周折呢?
### 为何要使用命名管道?
命名管道很少被使用的理由似乎很充分。毕竟在Unix系统上总有多种不同的方式完成同样的操作。有多种方式写文件、读文件、清空文件尽管命名管道比他们来得更高效。
值得注意的是,命名管道的内容驻留在内存中而不是被写到硬盘上。数据内容只有在输入输出端都打开时才会传送。用户可以在管道的输出端打开之前向管道多次写入。通过使用命名管道,用户可以创建一个同时有一个进程写入管道和一个进程读取管道的进程而不用关心协调二者时间上的同步。
用户可以创建一个单纯等待数据出现在输出端的管道,并在拿到输出数据后对其进行操作。
```
$ tail -f mypipe
```
一旦喂给管道数据的进程结束了,我们就可以看到一些输出。
```
$ tail -f mypipe
Uranus replicated to WCDC7
Saturn replicated to WCDC8
Pluto replicated to WCDC9
Server replication operation completed
```
如果研究一下向命名管道写入的进程,用户也许会惊讶于它的资源消耗之少。在下面的`ps`命令输出中唯一显著的资源消耗是虚拟内存VSZ那一列
```
ps u -P 80038
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
shs 80038 0.0 0.0 108488 764 pts/4 S 15:25 0:00 -bash
```
命名管道与Unix系统上更常用的管道相比足以不同到拥有另一个名号但是“管道”实在能反映出它们如何在进程间传送数据的形象故将称其为“命名管道”还真是恰如其分。也许您在执行操作时就能从这个聪明的Unix特性中获益匪浅呢。
--------------------------------------------------------------------------------
via: https://www.networkworld.com/article/3251853/linux/why-use-named-pipes-on-linux.html
作者:[Sandra Henry-Stocker][a]
译者:[YPBlib](https://github.com/YPBlib)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.networkworld.com/author/Sandra-Henry_Stocker/
[1]:http://www.networkworld.com/article/2926630/linux/11-pointless-but-awesome-linux-terminal-tricks.html#tk.nww-fsb