mirror of
https://github.com/LCTT/TranslateProject.git
synced 2026-08-23 04:03:29 +08:00
Merge remote-tracking branch 'LCTT/master'
This commit is contained in:
@@ -1,112 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to find and remove broken symlinks on Linux)
|
||||
[#]: via: (https://www.networkworld.com/article/3546252/how-to-find-and-remove-broken-symlinks-on-linux.html)
|
||||
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
|
||||
|
||||
How to find and remove broken symlinks on Linux
|
||||
======
|
||||
A symlink or "symbolic link" is a Linux file that simply points at another file. If the referenced file is removed, the symlink will remain but not indicate there's a problem until you try to use it. Here are some easy ways to find and remove symlinks that point to files that have been moved or removed.
|
||||
Thinkstock
|
||||
|
||||
Symbolic links play a very useful role on Linux systems. They can help you remember where important files are located on a system, make it easier for you to access those files and save you a good amount of disk space and trouble by making it unnecessary for you to copy large files just to make them a little more accessible.
|
||||
|
||||
### What exactly is a symbolic link?
|
||||
|
||||
Generally referred to as a "symlink" or "soft link", symbolic links are very small files. In fact, all a symlink really contains is the name of whatever file it points to, generally along with the file system path (relative to the current location or absolute). If a file named **ref1** points to a file named **/apps/refs/ref-2020**, **ref1** will be 19 characters long even if the ref-2020 file is 2 terabytes. If it points to **./ref-2020**, it will be only 10 characters in length. If it points to **ref-2020**, only eight.
|
||||
|
||||
If you issue a command like "vi ref1" (where ref1 is the name of a symlink), you will end up editing whatever file ref1 points to, not the contents of the symlink itself. Linux systems know how to work with symlinks and simply do the right thing. Similarly, if you use commands like **cat**, **more**, **head** or **tail**, you'll be looking at the content of the referenced file.
|
||||
|
||||
If you delete a symlink, on the other hand, you will be removing the link, never the referenced file. Again, Linux does what makes sense. Symlinks were meant to make using and sharing files easier -- nothing more.
|
||||
|
||||
#### When symlinks get broken
|
||||
|
||||
When the file that a symbolic link points to is removed from the system or renamed, the symlink will no longer function as intended. Being little more than a reference stored in some particular directory, the symlink isn't going to be updated or removed with changes to the file it points to. It keeps pointing at the referenced file, even after that file is long gone.
|
||||
|
||||
If you try to use a symlink that points to a non-existent file, you will get an error like this:
|
||||
|
||||
```
|
||||
$ tail whassup
|
||||
tail: cannot open 'whassup' for reading: No such file or directory
|
||||
```
|
||||
|
||||
If you try to access a symlink that points to itself (yes, stranger things have happened), you will see something like this:
|
||||
|
||||
```
|
||||
$ cat loopy
|
||||
cat: loopy: Too many levels of symbolic links
|
||||
$ ls -l loopy
|
||||
lrwxrwxrwx 1 shs shs 5 May 28 18:07 loopy -> loopy
|
||||
```
|
||||
|
||||
And, just in case that first letter in the long listing didn't catch your attention, it indicates that the file is a symbolic link. The **rwxrwxrwx** permissions are standard and don't reflect the permissions on the file the symlink points at.
|
||||
|
||||
### Finding broken symlinks
|
||||
|
||||
The find command has an option that allows you to locate symlinks that point to files that no longer exist. This command lists symlinks in the current directory:
|
||||
|
||||
```
|
||||
$ find . -type l
|
||||
```
|
||||
|
||||
The "l" (lowercase L) tells the find command to look for symbolic links.
|
||||
|
||||
The command shown below, on the other hand, looks in the current directory for symlinks that point to files that don't exist:
|
||||
|
||||
```
|
||||
$ find . -xtype l
|
||||
```
|
||||
|
||||
To avoid running into errors when the command tries to look into files or directories that you don't have permission to examine, you can send all error output to /dev/null like this:
|
||||
|
||||
```
|
||||
$ find . -xtype l 2>/dev/null
|
||||
```
|
||||
|
||||
You can also find broken symlinks with a command like this one. It's longer than the earlier one, but should do the same thing:
|
||||
|
||||
```
|
||||
$ find . -type l ! -exec test -e {} \; -print 2>/dev/null
|
||||
```
|
||||
|
||||
### What to do with broken symlinks
|
||||
|
||||
Unless you know that the file a symlink references is going to be replaced, the best move is to simply remove the broken link. In fact, you can find and remove broken symlinks in a single command if you want to, with a command like this one:
|
||||
|
||||
```
|
||||
$ find . -xtype l 2>/dev/null -exec rm {} \;
|
||||
```
|
||||
|
||||
The **rm {}** portion of that command turns into a "remove filename" command.
|
||||
|
||||
If, instead, you want to associate the symlink with a different file, you will have to remove the symlink and then recreate it so that it points to the new file. Here's an example:
|
||||
|
||||
```
|
||||
$ rm ref1
|
||||
$ ln -s /apps/data/newfile ref1
|
||||
```
|
||||
|
||||
#### Wrap-Up
|
||||
|
||||
Symbolic links make the referenced files easier to find and use, but they sometimes evolve into little more than road signs that advertise a diner that closed last year. Finding commands can help you get rid of the broken symlinks or alert you to the absence of files that you might still require.
|
||||
|
||||
Join the Network World communities on [Facebook][1] and [LinkedIn][2] to comment on topics that are top of mind.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.networkworld.com/article/3546252/how-to-find-and-remove-broken-symlinks-on-linux.html
|
||||
|
||||
作者:[Sandra Henry-Stocker][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.facebook.com/NetworkWorld/
|
||||
[2]: https://www.linkedin.com/company/network-world
|
||||
@@ -1,5 +1,5 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
|
||||
@@ -1,78 +1,68 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: reviewer: ( )
|
||||
[#]: translator: (wxy)
|
||||
[#]: reviewer: (wxy)
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to Manipulate an Ethernet Card Using the ethtool Command)
|
||||
[#]: via: (https://www.2daygeek.com/linux-ethtool-command-view-change-ethernet-adapter-settings-nic-card/)
|
||||
[#]: author: (Magesh Maruthamuthu https://www.2daygeek.com/author/magesh/)
|
||||
|
||||
How to Manipulate an Ethernet Card Using the ethtool Command
|
||||
如何使用 ethtool 命令管理以太网卡?
|
||||
======
|
||||
|
||||
Ethtool is used to view and modify network device driver parameters and hardware settings, especially for wired ethernet devices.
|
||||
`ethtool` 用于查看和修改网络设备(尤其是有线以太网设备)的驱动参数和硬件设置。你可以根据需要更改以太网卡的参数,包括自动协商、速度、双工和局域网唤醒等参数。通过对以太网卡的配置,你的计算机可以通过网络有效地进行通信。该工具提供了许多关于接驳到你的 Linux 系统的以太网设备的信息。
|
||||
|
||||
You can change ethernet card parameters as required, including auto-negotiation, Speed, Duplex and Wake-on LAN.
|
||||
在这篇文章中,我们将告诉你如何更改以下的参数以及如何查看这些参数。这篇文章将帮助你在 Linux 系统中排除与以太网卡相关的问题。
|
||||
|
||||
The configuration of your Ethernet card allows your computer to communicate effectively over the network.
|
||||
下面的信息将帮助你了解以太网卡的工作原理。
|
||||
|
||||
This tool provides many information about Ethernet devices connected to your Linux system.
|
||||
* **半双工**:半双工模式允许设备一次只能发送或接收数据包。
|
||||
* **全双工**:全双工模式允许设备可以同时发送和接收数据包。
|
||||
* **自动协商**:自动协商是一种机制,允许设备自动选择最佳网速和工作模式(全双工或半双工模式)。
|
||||
* **速度**:默认情况下,它会使用最大速度,你可以根据自己的需要改变它。
|
||||
* **链接检测**:链接检测可以显示网卡的状态。如果显示为 `no`,请尝试重启网卡。如果链路检测仍显示 `no`,则检查交换机与系统之间连接的线缆是否有问题。
|
||||
|
||||
In this article, we will show you how to change the below parameters and how to view them.
|
||||
### 如何在 Linux 上安装 ethtool
|
||||
|
||||
This article will help you troubleshoot Ethernet card related problems on a Linux system.
|
||||
默认情况下,大多数系统上应该已经安装了 `ethtool`。如果没有,你可以从发行版的官方版本库中安装。
|
||||
|
||||
The following information will help you understand how Ethernet card works.
|
||||
|
||||
* **Half Duplex:** Half-duplex mode allows a device to either send or receive packets at a time.
|
||||
* **Full Duplex:** Full-duplex mode allows a device to send and receive packets simultaneously.
|
||||
* **Auto-Negotiation:** Auto-negotiation is a mechanism that allows a device to automatically choose the best network speed and mode of operation (full-duplex or half-dual mode).
|
||||
* **Speed:** By default it uses maximum speed and you can change it according to your need.
|
||||
* **Link detection:** Link detection shows the status of the network interface card. If it shows “no” then try restarting the interface. If the link detection still says “no”, check if there are any issues with the cables connected between the switch and the system.
|
||||
|
||||
|
||||
|
||||
### How to Install ethtool on Linux
|
||||
|
||||
By default ethtool should already be installed on most systems. If not, you can install it from the distribution official repository.
|
||||
|
||||
For **RHEL/CentOS 6/7** systems, use the **[yum command][1]** to install ethtool.
|
||||
对于 RHEL/CentOS 6/7 系统,请使用 [yum 命令][1] 安装 `ethtool`:
|
||||
|
||||
```
|
||||
$ sudo yum install -y ethtool
|
||||
```
|
||||
|
||||
For **RHEL/CentOS 8** and **Fedora** systems, use the **[dnf command][2]** to install ethtool.
|
||||
对于 RHEL/CentOS 8 和 Fedora 系统,请使用 [dnf 命令][2] 安装 `ethtool`:
|
||||
|
||||
```
|
||||
$ sudo yum install -y ethtool
|
||||
```
|
||||
|
||||
For **Debian** based systems, use the **[apt command][3]** or **[apt-get command][4]** to install ethtool.
|
||||
对于基于 Debian 的系统,请使用 [apt 命令][3] 或 [apt-get 命令][4] 安装 `ethtool`:
|
||||
|
||||
```
|
||||
$ sudo apt-get install ethtool
|
||||
```
|
||||
|
||||
For **openSUSE** systems, use the **[zypper command][5]** to install ethtool.
|
||||
对于 openSUSE 系统,使用 [zypper 命令][5] 安装 `ethtool`:
|
||||
|
||||
```
|
||||
$ sudo zypper install -y ethtool
|
||||
```
|
||||
|
||||
For **Arch Linux** systems, use the **[pacman command][6]** to install ethtool.
|
||||
对于 Arch Linux 系统,使用 [pacman 命令][6] 安装 `ethtool`:
|
||||
|
||||
```
|
||||
$ sudo pacman -S ethtool
|
||||
```
|
||||
|
||||
### How to Check the Available Network Interface on Linux
|
||||
### 如何检查 Linux 上的可用网络接口
|
||||
|
||||
You can use the **[ip command][7]** or the **ifconfig command** (deprecated in modern distribution) to verify the name and other details of the available and active network interfaces.
|
||||
你可以使用 [ip 命令][7]或 `ifconfig` 命令(在现代发行版中已被淘汰)来验证可用的、活动的网卡的名称和其他细节:
|
||||
|
||||
```
|
||||
# ip a
|
||||
or
|
||||
或
|
||||
# ifconfig
|
||||
|
||||
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
|
||||
@@ -85,16 +75,14 @@ or
|
||||
valid_lft forever preferred_lft forever
|
||||
```
|
||||
|
||||
### How to Check Network Interface Card (NIC) Information on Linux
|
||||
### 如何检查 Linux 上的网卡(NIC)信息
|
||||
|
||||
Once you have the Ethernet interface name, you can easily check the details of it using the ethtool command as shown below.
|
||||
|
||||
On Linux, each network interface card (NIC) is assigned unique names, such as ethX, enpXXX, and so on.
|
||||
|
||||
* The older Linux distribution used the **eth[X]** format. For example, RHEL 6 and their older versions.
|
||||
* Modern Linux distributions use **enp[XXX]** or **ens[XXX]** formats. For example, most of the modern Linux distribution uses this format, including RHEL 7, Debian 10, Ubuntu 16.04 LTS.
|
||||
掌握了以太网卡名称后,就可以使用 `ethtool` 命令轻松查看其详细信息,如下所示。
|
||||
|
||||
在 Linux 系统中,每个网卡(NIC)都被分配了唯一的名称,如 ethX、enpXXX 等。
|
||||
|
||||
* 旧的 Linux 发行版使用的是 `eth[X]` 格式。例如,RHEL 6 和它们的旧版本。
|
||||
* 现代的 Linux 发行版使用 `enp[XXX]` 或 `ens[XXX]` 格式。例如,大多数现代 Linux 发行版都使用这种格式,包括 RHEL 7、Debian 10、Ubuntu 16.04 LTS。
|
||||
|
||||
```
|
||||
# ethtool eth0
|
||||
@@ -122,9 +110,9 @@ Settings for eth0:
|
||||
Link detected: yes
|
||||
```
|
||||
|
||||
### How to Check Ethernet Card Driver and Firmware Version on Linux
|
||||
### 如何检查以太网卡的驱动程序和固件版本
|
||||
|
||||
You can check driver version, firmware version, and bus details using the ethtool command with the **“-i”** option as shown below.
|
||||
你可以使用 `ethtool` 命令的 `-i` 选项检查驱动程序版本、固件版本和总线的详细信息,如下所示:
|
||||
|
||||
```
|
||||
# ethtool -i eth0
|
||||
@@ -141,9 +129,9 @@ supports-register-dump: yes
|
||||
supports-priv-flags: no
|
||||
```
|
||||
|
||||
### How to Check Network Usage Statistics on Linux
|
||||
### 如何检查网络使用情况统计
|
||||
|
||||
You can view network usage statistics using the ethtool command with the **“-S”** option. It shows the bytes transferred, received, errors etc.
|
||||
你可以使用 `ethtool` 命令中的 `-S` 选项来查看网络使用情况统计。它可以显示传输的字节数、接收的字节数、错误数等。
|
||||
|
||||
```
|
||||
# ethtool -S eth0
|
||||
@@ -222,27 +210,27 @@ NIC statistics:
|
||||
tx timeout count: 0
|
||||
```
|
||||
|
||||
### How to Change the Speed of Ethernet Device on Linux
|
||||
### 如何改变以太网设备的速度
|
||||
|
||||
You can change the speed of the Ethernet as needed. When you make this change the interface will automatically go offline and you will need to bring it back online using the **[ifup command][8]** or the ip command or the nmcli command.
|
||||
你可以根据需要改变以太网的速度。当你进行此更改时,网卡将自动掉线,你需要使用 [ifup 命令][8] 或 `ip` 命令或 `nmcli` 命令将其重新上。
|
||||
|
||||
```
|
||||
# ethtool -s eth0 speed 100
|
||||
# ip link set eth0 up
|
||||
```
|
||||
|
||||
### How to Enable/Disable Auto-Negotiation for Ethernet Device on Linux
|
||||
### 如何在 Linux 上启用/禁用以太网卡的自动协商?
|
||||
|
||||
You can enable or disable Auto-Negotiation using the ethtool command with the **“autoneg”** option as shown below.
|
||||
你可以使用 `ethtool` 命令中的 `autoneg` 选项启用或禁用自动协商,如下图所示:
|
||||
|
||||
```
|
||||
# ethtool -s eth0 autoneg off
|
||||
# ethtool -s eth0 autoneg on
|
||||
```
|
||||
|
||||
### How to Change Multiple Parameters at Once
|
||||
### 如何同时更改多个参数
|
||||
|
||||
If you want to change multiple parameters of the Ethernet interface simultaneously using the ethtool command, use the format below.
|
||||
如果你想使用 `ethtool` 命令同时更改以太网卡的多个参数,请使用下面的格式:
|
||||
|
||||
```
|
||||
Syntax:
|
||||
@@ -253,29 +241,29 @@ ethtool –s [device_name] speed [10/100/1000] duplex [half/full] autoneg [on/of
|
||||
# ethtool –s eth0 speed 1000 duplex full autoneg off
|
||||
```
|
||||
|
||||
### How to Check Auto-negotiation, RX and TX of a Particular Interface on Linux
|
||||
|
||||
To view auto-negotiation details about a specific Ethernet device, use the below format.
|
||||
### 如何检查特定网卡的自动协商、RX 和 TX
|
||||
|
||||
要查看关于特定以太网设备的自动协商等详细信息,请使用以下格式:
|
||||
|
||||
```
|
||||
# ethtool -a eth0
|
||||
```
|
||||
|
||||
### How to Identify a Specific NIC from Multiple Devices (Blink LED Port of NIC Card)
|
||||
### 如何从多个设备中识别出特定的网卡(闪烁网卡上的 LED)
|
||||
|
||||
This option is very useful if you want to identify a specific physical interface port among others. The below ethtool command blink the LED of the eth0 port.
|
||||
如果你想识别一个特定的物理接口,这个选项非常有用。下面的 `ethtool` 命令会使 `eth0` 端口的 LED 灯闪烁:
|
||||
|
||||
```
|
||||
# ethtool -p eth0
|
||||
```
|
||||
|
||||
### How to Set These Parameters in Linux Permanently
|
||||
### 如何在 Linux 中永久设置这些参数
|
||||
|
||||
After a system restarts the changes you made with ethtool will be reverted by default.
|
||||
在系统重启后,你使用 `ethtool` 所做的更改将被默认恢复。
|
||||
|
||||
To make custom settings permanent, you need to update your value in the network configuration file. Depending on your Linux distribution you may need to update this value to the correct file.
|
||||
要使自定义设置永久化,你需要更新网络配置文件中的值。根据你的 Linux 发行版,你可能需要将此值更新到正确的文件中。
|
||||
|
||||
For RHEL based systems. You must use the ETHTOOL_OPTS variables.
|
||||
对于基于 RHEL 的系统。你必须使用 `ETHTOOL_OPTS` 变量:
|
||||
|
||||
```
|
||||
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
|
||||
@@ -283,7 +271,7 @@ For RHEL based systems. You must use the ETHTOOL_OPTS variables.
|
||||
ETHTOOL_OPTS="speed 1000 duplex full autoneg off"
|
||||
```
|
||||
|
||||
For **Debian** based systems.
|
||||
对于基于 Debian 的系统:
|
||||
|
||||
```
|
||||
# vi /etc/network/interfaces
|
||||
@@ -297,8 +285,8 @@ via: https://www.2daygeek.com/linux-ethtool-command-view-change-ethernet-adapter
|
||||
|
||||
作者:[Magesh Maruthamuthu][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
译者:[wxy](https://github.com/wxy)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to find and remove broken symlinks on Linux)
|
||||
[#]: via: (https://www.networkworld.com/article/3546252/how-to-find-and-remove-broken-symlinks-on-linux.html)
|
||||
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
|
||||
|
||||
如何在 Linux 上查找和删除损坏的符号链接
|
||||
======
|
||||
符号链接 (symbolic link) 是指向另一个文件的 Linux 文件。如果删除了引用的文件,符号链接将保留,除非你尝试使用它,否则不会有问题。以下是查找和删除指向文件已被删除的符号链接的简单方法。
|
||||
|
||||
符号链接在 Linux 系统上扮演了非常有用的角色。它们可以帮助你记住重要文件在系统上的位置,让你不必为了更方便访问大文件而复制它们,从而更方便地访问它们并节省了大量的空间。
|
||||
|
||||
### 什么是符号链接?
|
||||
|
||||
通常称它们为 “symlink” 或“软链接”,符号链接是非常小的文件。实际上,符号链接真正包含的是它指向文件的名称,通常包含路径(相对于当前位置或绝对路径)。如果有个名为 **ref1** 的文件指向名为 **/apps/refs/ref-2020** 的文件,那么 **ref1** 的长度将为 19 个字符,即使 ref-202 文件有 2TB。如果指向 **./ref-2020**,那么长度仅为 10 个字符。如果指向 **ref-2020**,那么只有八个。
|
||||
|
||||
如果你执行 “vi ref1” 之类的命令(其中 ref1 是符号链接的名称),你将编辑 ref1 指向的文件,而不是符号链接本身的内容。Linux 系统知道如何使用符号链接,并且可以做正确的事。同样,如果你使用诸如 **cat**、**more**、**head** 或 **tail** 之类的命令,那么将查看引用文件的内容。
|
||||
|
||||
另一方面,如果删除符号链接,你将删除链接,而不是引用的文件。再说一次,Linux 知道怎么做。符号链接使得使用和共享文件更加容易,仅此而已。
|
||||
|
||||
#### 符号链接损坏时
|
||||
|
||||
当删除或重命名符号链接指向的文件时,符号链接将不再起作用。符号链接只不过是存储在某个特定目录中的引用而已,它不会随着指向它的文件的更改而更新或删除。即使该文件已经消失了很长时间,它仍然指向被引用的文件。
|
||||
|
||||
如果尝试使用指向不存在的文件的符号链接,那么将出现如下错误:
|
||||
|
||||
```
|
||||
$ tail whassup
|
||||
tail: cannot open 'whassup' for reading: No such file or directory
|
||||
```
|
||||
|
||||
如果你尝试访问指向自身的符号链接(是的,发生了陌生的事情),你将看到类似以下的内容:
|
||||
|
||||
```
|
||||
$ cat loopy
|
||||
cat: loopy: Too many levels of symbolic links
|
||||
$ ls -l loopy
|
||||
lrwxrwxrwx 1 shs shs 5 May 28 18:07 loopy -> loopy
|
||||
```
|
||||
|
||||
以防万一,如果列表中的第一个字母没有引起你的注意,这表示该文件是符号链接。**rwxrwxrwx** 权限是标准权限,并不反映符号链接指向的文件的权限。
|
||||
|
||||
### 查找损坏的符号链接
|
||||
|
||||
find 命令有一个选项,能让你找到指向不再存在的文件的符号链接。此命令列出当前目录中的符号链接:
|
||||
|
||||
```
|
||||
$ find . -type l
|
||||
```
|
||||
|
||||
“ l”(小写字母 L)告诉 find 命令查找符号链接。
|
||||
|
||||
另一方面,下面的命令在当前目录中查找指向不存在的文件的符号链接:
|
||||
|
||||
```
|
||||
$ find . -xtype l
|
||||
```
|
||||
|
||||
为了避免在命令尝试查找你无权检查的文件或目录时发生错误,你可以将所有错误输出到 /dev/null,如下所示:
|
||||
|
||||
```
|
||||
$ find . -xtype l 2>/dev/null
|
||||
```
|
||||
|
||||
你也可以使用此命令找到损坏的符号链接。它比以前的更长,但会做同样的事情:
|
||||
|
||||
```
|
||||
$ find . -type l ! -exec test -e {} \; -print 2>/dev/null
|
||||
```
|
||||
|
||||
### 如何处理损坏的符号链接
|
||||
|
||||
除非你知道符号链接引用的文件将被替换,否则最好的方法是直接删除损坏的链接。实际上,如果需要,你可以使用以下命令在单个命令中查找和删除损坏的符号链接:
|
||||
|
||||
```
|
||||
$ find . -xtype l 2>/dev/null -exec rm {} \;
|
||||
```
|
||||
|
||||
该命令的 **rm {}** 部分是“删除文件名”命令
|
||||
|
||||
相反,如果你想将符号链接与其他文件相关联,你必须先删除该符号链接,然后重新创建它,使其指向新文件。这是一个例子:
|
||||
|
||||
```
|
||||
$ rm ref1
|
||||
$ ln -s /apps/data/newfile ref1
|
||||
```
|
||||
|
||||
#### 总结
|
||||
|
||||
符号链接使引用的文件更易于查找和使用,但有时它会比路标上那些去年关闭的餐馆广告还要多。查找命令可以帮助你摆脱损坏的符号链接,或在缺少可能仍需要的文件时提醒你。
|
||||
|
||||
加入 [Facebook][1] 和 [LinkedIn][2] 上的 Network World 社区,评论热门主题。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.networkworld.com/article/3546252/how-to-find-and-remove-broken-symlinks-on-linux.html
|
||||
|
||||
作者:[Sandra Henry-Stocker][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.facebook.com/NetworkWorld/
|
||||
[2]: https://www.linkedin.com/company/network-world
|
||||
@@ -7,16 +7,17 @@
|
||||
[#]: via: (https://fedoramagazine.org/use-fastapi-to-build-web-services-in-python/)
|
||||
[#]: author: (Clément Verna https://fedoramagazine.org/author/cverna/)
|
||||
|
||||
Use FastAPI to build web services in Python
|
||||
|
||||
使用 Python FastAPI 构建 Web 服务
|
||||
======
|
||||
|
||||
![][1]
|
||||
|
||||
_[FastAPI][2]_ is a modern Python web framework that leverage the latest Python improvement in asyncio. In this article you will see how to set up a container based development environment and implement a small web service with FastAPI.
|
||||
[FastAPI][2] 是一个使用 Python 编写的 Web 框架,还应用了 Python asyncio 库中最新的优化。本文将会介绍如何搭建基于容器的开发环境,还会展示如何使用 FastAPI 实现一个小型 Web 服务。
|
||||
|
||||
### Getting Started
|
||||
### 起步
|
||||
|
||||
The development environment can be set up using the Fedora container image. The following Dockerfile prepares the container image with FastAPI, [Uvicorn][3] and [aiofiles][4].
|
||||
我们将使用 Fedora 作为基础镜像来搭建开发环境,并使用 Dockerfile 为镜像注入 FastAPI、[Uvicorn][3] 和 [aiofiles][4] 这几个包。
|
||||
|
||||
```
|
||||
FROM fedora:32
|
||||
@@ -27,7 +28,7 @@ WORKDIR /srv
|
||||
CMD ["uvicorn", "main:app", "--reload"]
|
||||
```
|
||||
|
||||
After saving this Dockerfile in your working directory, build the container image using podman.
|
||||
在工作目录下保存 Dockerfile 之后,执行 `podman` 命令构建容器镜像。
|
||||
|
||||
```
|
||||
$ podman build -t fastapi .
|
||||
@@ -36,7 +37,7 @@ REPOSITORY TAG IMAGE ID CREATED SIZE
|
||||
localhost/fastapi latest 01e974cabe8b 18 seconds ago 326 MB
|
||||
```
|
||||
|
||||
Now let’s create a basic FastAPI program and run it using that container image.
|
||||
下面我们可以开始创建一个简单的 FastAPI 应用程序,并通过容器镜像运行。
|
||||
|
||||
```
|
||||
from fastapi import FastAPI
|
||||
@@ -48,7 +49,7 @@ async def root():
|
||||
return {"message": "Hello Fedora Magazine!"}
|
||||
```
|
||||
|
||||
Save that source code in a _main.py_ file and then run the following command to execute it:
|
||||
将上面的代码保存到 `main.py` 文件中,然后执行以下命令开始运行:
|
||||
|
||||
```
|
||||
$ podman run --rm -v $PWD:/srv:z -p 8000:8000 --name fastapi -d fastapi
|
||||
@@ -56,33 +57,31 @@ $ curl http://127.0.0.1:8000
|
||||
{"message":"Hello Fedora Magazine!"
|
||||
```
|
||||
|
||||
You now have a running web service using FastAPI. Any changes to _main.py_ will be automatically reloaded. For example, try changing the “Hello Fedora Magazine!” message.
|
||||
这样,一个基于 FastAPI 的 Web 服务就跑起来了。由于指定了 `--reload` 参数,一旦 `main.py` 文件发生了改变,整个应用都会自动重新加载。你可以尝试将返回信息 `"Hello Fedora Magazine!"` 修改为其它内容,然后观察效果。
|
||||
|
||||
To stop the application, run the following command.
|
||||
可以使用以下命令停止应用程序:
|
||||
|
||||
```
|
||||
$ podman stop fastapi
|
||||
```
|
||||
|
||||
### Building a small web service
|
||||
### 构建一个小型 Web 服务
|
||||
|
||||
To really see the benefits of FastAPI and the performance improvement it brings ([see comparison][5] with other Python web frameworks), let’s build an application that manipulates some I/O. You can use the output of the _dnf history_ command as data for that application.
|
||||
接下来我们会构建一个需要 I/O 操作的应用程序,通过这个应用程序,我们可以看到 FastAPI 自身的特点,以及它在性能上有什么优势(可以在[这里][5]参考 FastAPI 和其它 Python Web 框架的对比)。为简单起见,我们直接使用 `dnf history` 命令的输出来作为这个应用程序使用的数据。
|
||||
|
||||
First, save the output of that command in a file.
|
||||
首先将 `dnf history` 命令的输出保存到文件。
|
||||
|
||||
```
|
||||
$ dnf history | tail --lines=+3 > history.txt
|
||||
```
|
||||
|
||||
The command is using _tail_ to remove the headers of _dnf history_ which are not needed by the application. Each dnf transaction can be represented with the following information:
|
||||
在上面的命令中,我们使用 `tail` 去除了 `dnf history` 输出内容中无用的表头信息。剩余的每一条 `dnf` 事务都包括了以下信息:
|
||||
|
||||
* id : number of the transaction (increments every time a new transaction is run)
|
||||
* command : the dnf command run during the transaction
|
||||
* date: the date and time the transaction happened
|
||||
* id:事务编号(每次运行一条新事务时该编号都会递增)
|
||||
* command:事务中运行的 `dnf` 命令
|
||||
* date:执行事务的日期和时间
|
||||
|
||||
|
||||
|
||||
Next, modify the _main.py_ file to add that data structure to the application.
|
||||
然后修改 `main.py` 文件将相关的数据结构添加进去。
|
||||
|
||||
```
|
||||
from fastapi import FastAPI
|
||||
@@ -96,9 +95,9 @@ class DnfTransaction(BaseModel):
|
||||
date: str
|
||||
```
|
||||
|
||||
FastAPI comes with the [pydantic][6] library which allow you to easily build data classes and benefit from type annotation to validate your data.
|
||||
FastAPI 自带的 [pydantic][6] 库让你可以轻松定义一个数据类,其中的类型注释对数据的验证也提供了方便。
|
||||
|
||||
Now, continue building the application by adding a function that will read the data from the _history.txt_ file.
|
||||
再增加一个函数,用于从 `history.txt` 文件中读取数据。
|
||||
|
||||
```
|
||||
import aiofiles
|
||||
@@ -125,9 +124,9 @@ async def read_history():
|
||||
return transactions
|
||||
```
|
||||
|
||||
This function makes use of the _[aiofiles][4]_ library which provides an asyncio API to manipulate files in Python. This means that opening and reading the file will not block other requests made to the server.
|
||||
这个函数中使用了 `aiofiles` 库,这个库提供了一个异步 API 来处理 Python 中的文件,因此打开文件或读取文件的时候不会阻塞其它对服务器的请求。
|
||||
|
||||
Finally, change the root function to return the data stored in the transactions list.
|
||||
最后,修改 `root` 函数,让它返回事务列表中的数据。
|
||||
|
||||
```
|
||||
@app.get("/")
|
||||
@@ -135,7 +134,7 @@ async def read_root():
|
||||
return await read_history()
|
||||
```
|
||||
|
||||
To see the output of the application, run the following command
|
||||
执行以下命令就可以看到应用程序的输出内容了。
|
||||
|
||||
```
|
||||
$ curl http://127.0.0.1:8000 | python -m json.tool
|
||||
@@ -159,23 +158,24 @@ $ curl http://127.0.0.1:8000 | python -m json.tool
|
||||
]
|
||||
```
|
||||
|
||||
### Conclusion
|
||||
### 总结
|
||||
|
||||
_FastAPI_ is gaining a lot a popularity in the Python web framework ecosystem because it offers a simple way to build web services using asyncio. You can find more information about _FastAPI_ in the [documentation][2].
|
||||
FastAPI 提供了一种使用 asyncio 构建 Web 服务的简单方法,因此它在 Python Web 框架的生态中日趋流行。要了解 FastAPI 的更多信息,欢迎查阅 [FastAPI 文档][2]。
|
||||
|
||||
The code of this article is available in [this GitHub repository][7].
|
||||
本文中的代码可以在 [GitHub][7] 上找到。
|
||||
|
||||
* * *
|
||||
|
||||
_Photo by [Jan Kubita][8] on [Unsplash][9]._
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://fedoramagazine.org/use-fastapi-to-build-web-services-in-python/
|
||||
|
||||
作者:[Clément Verna][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
译者:[HankChow](https://github.com/HankChow)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
Reference in New Issue
Block a user