在Linux中如何使用wget命令?
在本教程中,让我们学习如何在Linux中使用wget命令下载文件。
对于任何Linux用户,下载文件都是一项常见的任务。
虽然可以使用浏览器下载内容,但了解使用命令行在Linux中下载文件的步骤会很有帮助。
这是wget命令进入图片的地方。
Linux中wget命令的基础
wget实用程序预先安装在大多数Linux发行版中。
但是,如果您的系统不是这种情况,则可以使用程序包管理器进行下载以进行分发。
为了检查这一点,我们将要求系统显示wget命令的版本。
可以通过在终端中键入此命令来完成。
wget --version
Wget版本
如果未在系统上显示wget命令的版本,则需要安装它。
在开始在Linux中安装wget命令之前,我们需要确保所有系统信息库都是最新的。
为此,我们需要使用apt软件包管理服务更新默认存储库。
在系统上打开命令行,然后键入以下内容。
sudo apt update
现在,您可以根据所使用的发行版在系统上安装wget命令。
对于基于Debian和Ubuntu的系统,请使用以下命令。
sudo apt install wget
对于基于CentOS和基于Fedora的系统,请使用以下命令。
sudo yum install wget
了解wget命令
Linux中的wget命令是用于从Web下载文件的命令。
此命令使用文件的URL进行下载。
该命令可用于通过HTTP,FTP和HTTPS协议下载数据。
我们可以使用它直接从命令行下载文件,而无需Web浏览器。
此外,wget是非交互式实用程序。
这使用户可以在后台下载文件。
即使用户已注销,它也可以工作。
这在下载大文件的情况下非常重要。
如果下载失败,则wget实用程序将反复尝试恢复它,直到下载完整的文件为止。
如果服务器允许,它将从上次停止的地方继续下载。
在Linux中使用wget命令
理解任何命令的最佳方法是通过了解其语法。
这是Linux中wget命令的语法。
wget [option] [URL]
其中我们使用要下载的文件的URL。
wget实用程序允许使用几个选项来设置我们的下载。
这是Linux中wget命令提供的一些常用选项的列表。
Option | Effect |
-h | This option is used to display the help message for the wget command. It includes all the possible options which can be used with the wget command in Linux |
-b | This option is used to send the download process to the background as soon as it is initiated with the wget command. It frees up your terminal session to run other commands. |
-i | This option is used to read the URL for the wget command from a file. It eliminates the need for the wget command to have an URL and allows the inclusion of an input file. |
-o | Name of the output file if you do not want the same name as the server has provided |
-c | This tag makes wget keep a track of the download progress in case of download interruptions. We can continue an interrupted download if this option was used when the download was initiated. |
-tries=n | This option is used to limit the number of tries for the wget command. Using this command will cause the wget utility to retry a download only ‘n’ times. The default is 20. |
-limit-rate=maxlim | This option is used to limit the download speed for the wget command. This helps the user to dictate the amount of bandwidth to be allocated for a download. |
wget command in Linux
该列表涵盖了wget实用程序中使用的基本选项。
您应该使用-h选项浏览帮助页面,以了解wget命令的全部功能。
如何使用wget命令下载文件?
现在,我们已经了解了Linux中的wget命令及其参数。
现在是时候使用这些知识通过wget命令下载文件了。
对于本教程,我们将介绍一些示例。
通过HTTP下载文件
wget实用程序最简单的用法是下载一个文件。
对于此示例,我们将下载一个iso文件。
wget -c <file URL>
在上面的示例中,我正在下载Kali Linux ISO文件以及-c标签进行演示。
由于这是一个很大的文件,因此如果由于任何原因中断了ISO文件下载,我可以在同一目录中运行相同的命令,下载将从中断处继续进行。
从FTP服务器下载文件
您无需使用ftp命令连接到FTP服务器即可下载文件。
这可以在使用wget实用程序时完成。
从HTTP下载文件与FTP服务器下载文件的区别在于,可能需要输入用户名和密码。
让我们使用wget命令登录FTP服务器并下载文件。
wget --user='FTP username' --password='FTP server password' <FTP URL>