2 种从 Linux 终端下载文件的方法
下载
如果你被困在 Linux 终端，比如说在服务器上，如何从终端下载文件？


在 Linux 中是没有 `download` 命令的，但是有几个 Linux 命令可用于下载文件。


在这篇终端技巧中，你将学习两种在 Linux 中使用命令行下载文件的方法。


我在这里使用的是 Ubuntu，但除了安装，其余的命令同样适用于所有其他 Linux 发行版。


### 使用 wget 命令从 Linux 终端下载文件


![](/data/attachment/album/202010/25/112653xu2w2cc7ssp5yq6x.png)


[wget](https://www.gnu.org/software/wget/) 也许是 Linux 和类 UNIX 系统中使用最多的命令行下载管理器。你可以使用 `wget` 下载一个文件、多个文件、整个目录甚至整个网站。


`wget` 是非交互式的，可以轻松地在后台工作。这意味着你可以很容易地在脚本中使用它，甚至构建像 [uGet 下载管理器](https://itsfoss.com/install-latest-uget-ubuntu-linux-mint/)这样的工具。


让我们看看如何使用 `wget` 从终端下载文件。


#### 安装 wget


大多数 Linux 发行版都预装了 `wget`。它也可以在大多数发行版的仓库中找到，你可以使用发行版的包管理器轻松安装它。


在基于 Ubuntu 和 Debian 的发行版上，你可以使用 [apt 包管理器](https://itsfoss.com/apt-command-guide/)命令：



```
sudo apt install wget

```

#### 使用 wget 下载文件或网页


你只需要提供文件或网页的 URL。它将在你所在的目录下以原始名下载该文件。



```
wget URL

```

![](/data/attachment/album/202010/25/112659fxda0lthgcchug4p.png)


要下载多个文件，你必须将它们的 URL 保存在一个文本文件中，并将该文件作为输入提供给 `wget`，就像这样：



```
wget -i download_files.txt

```

#### 用 wget 下载不同名字的文件


你会注意到，网页在 `wget` 中几乎总是以 `index.html` 的形式保存。为下载的文件提供自定义名称是个好主意。


你可以在下载时使用 `-O` （大写字母 `O`） 选项来提供输出文件名：



```
wget -O filename URL

```

![](/data/attachment/album/202010/25/112414rvk25kjjk5cjy5by.png)


#### 用 wget 下载一个文件夹


假设你正在浏览一个 FTP 服务器，你需要下载整个目录，你可以使用递归选项 `-r`：



```
wget -r ftp://server-address.com/directory

```

#### 使用 wget 下载整个网站


是的，你完全可以做到这一点。你可以用 `wget` 镜像整个网站。我说的下载整个网站是指整个面向公众的网站结构。


虽然你可以直接使用镜像选项 `-m`，但最好加上：


* `–convert-links`：链接将被转换，使内部链接指向下载的资源，而不是网站。
* `–page-requisites`：下载额外的东西，如样式表，使页面在离线状态下看起来更好。



```
wget -m --convert-links --page-requisites website_address

```

![](/data/attachment/album/202010/25/112416h9jgg43t7hul7mt5.png)


#### 额外提示：恢复未完成的下载


如果你因为某些原因按 `CTRL-C` 键中止了下载，你可以用选项 `-c` 恢复之前的下载：



```
wget -c

```

### 使用 curl 在 Linux 命令行中下载文件


和 `wget` 一样，[curl](https://curl.haxx.se/) 也是 Linux 终端中最常用的下载文件的命令之一。[使用 curl](https://linuxhandbook.com/curl-command-examples/) 的方法有很多，但我在这里只关注简单的下载。


#### 安装 curl


虽然 `curl` 并不是预装的，但在大多数发行版的官方仓库中都有。你可以使用你的发行版的包管理器来安装它。


要[在 Ubuntu](https://itsfoss.com/install-curl-ubuntu/) 和其他基于 Debian 的发行版上安装 `curl`，请使用以下命令：



```
sudo apt install curl

```

#### 使用 curl 下载文件或网页


如果你在使用 `curl` 命令时没有在 URL 中带任何选项，它就会读取文件并打印在终端上。


要在 Linux 终端中使用 `curl` 命令下载文件，你必须使用 `-O`（大写字母 `O`）选项：



```
curl -O URL

```

![](/data/attachment/album/202010/25/112701arpm5ii5rpm3obrd.png)


在 Linux 中，用 `curl` 下载多个文件是比较简单的。你只需要指定多个 URL 即可：



```
curl -O URL1 URL2 URL3

```

请记住，`curl` 不像 `wget` 那么简单。`wget` 可以将网页保存为 `index.html`，`curl` 却会抱怨远程文件没有网页的名字。你必须按照下一节的描述用一个自定义的名字来保存它。


#### 用不同的名字下载文件


这可能会让人感到困惑，但如果要为下载的文件提供一个自定义的名称（而不是原始名称），你必须使用 `-o`（小写 `O`）选项：



```
curl -o filename URL

```

![](/data/attachment/album/202010/25/112422gsj0sws9scwsjr2v.png)


有些时候，`curl` 并不能像你期望的那样下载文件，你必须使用选项 `-L`（代表位置）来正确下载。这是因为有些时候，链接会重定向到其他链接，而使用选项 `-L`，它就会跟随最终的链接。


#### 用 curl 暂停和恢复下载


和 `wget` 一样，你也可以用 `curl` 的 `-c` 选项恢复暂停的下载：



```
curl -c URL

```

### 总结


和以往一样，在 Linux 中做同一件事有多种方法。从终端下载文件也不例外。


`wget` 和 `curl` 只是 Linux 中最流行的两个下载文件的命令。还有更多这样的命令行工具。基于终端的网络浏览器，如 [elinks](http://elinks.or.cz/)、[w3m](http://w3m.sourceforge.net/) 等也可以用于在命令行下载文件。


就个人而言，对于一个简单的下载，我更喜欢使用 `wget` 而不是 `curl`。它更简单，也不会让你感到困惑，因为你可能很难理解为什么 `curl` 不能以预期的格式下载文件。


欢迎你的反馈和建议。




---


via: <https://itsfoss.com/download-files-from-linux-terminal/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

If you are stuck to the Linux terminal, say on a server, how do you download a file from the terminal?

There is no download command in Linux but there are a couple of Linux commands for downloading file.

In this terminal trick, you’ll learn two ways to download files using the command line in Linux.

I am using Ubuntu here but apart from the installation, the rest of the commands are equally valid for all other Linux distributions.

## Download files from Linux terminal using wget command

[wget](https://www.gnu.org/software/wget/?ref=itsfoss.com) is perhaps the most used command line [download manager for Linux](https://itsfoss.com/32-bit-linux-distributions/) and UNIX-like systems. You can download a single file, multiple files, an entire directory, or even an entire website using wget.

wget is non-interactive and can easily work in the background. This means you can easily use it in scripts or even build tools like [uGet download manager](https://itsfoss.com/install-latest-uget-ubuntu-linux-mint/).

Let’s see how to use wget to download files from terminal.

### Installing wget

Most Linux distributions come with wget preinstalled. It is also available in the repository of most distributions and you can easily install it using your distribution’s package manager.

On Ubuntu and Debian based distributions, you can use the [apt package manager](https://itsfoss.com/apt-command-guide/) command:

`sudo apt install wget`


### Download a file or webpage using wget

You just need to provide the URL of the file or webpage. It will download the file with its original name in the directory you are in.

`wget URL`


![Download File In Linux Terminal Using Wget](https://itsfoss.com/content/images/wordpress/2020/10/download-file-in-linux-terminal-using-wget.png)

To download multiple files, you’ll have to save their URLs in a text file and provide that text file as input to wget like this:

`wget -i download_files.txt`


### Download files with a different name using wget

You’ll notice that a webpage is almost always saved as index.html with wget. It will be a good idea to provide custom name to downloaded file.

You can use the -O (uppercase O) option to provide the output filename while downloading.

`wget -O filename URL`


![Specify file name while downloading file using wget Linux command](https://itsfoss.com/content/images/wordpress/2020/10/download-file-in-linux-terminal-using-wget-2.png)

### Download a folder using wget

Suppose you are browsing an FTP server and you need to download an entire directory, you can use the recursive option

`wget -r ftp://server-address.com/directory`


### Download an entire website using wget

Yes, you can totally do that. You can mirror an entire website with wget. By downloading an entire website I mean the entire public facing website structure.

While you can use the mirror option -m directly, it will be a good idea add:

- –convert-links : links are converted so that internal links are pointed to downloaded resource instead of web
- –page-requisites: downloads additional things like style sheets so that the pages look better offline

`wget -m --convert-links --page-requisites website_address`


![Download Entire Website Using Wget](https://itsfoss.com/content/images/wordpress/2020/10/download-entire-website-using-wget.png)

### Bonus Tip: Resume incomplete downloads

If you aborted the download by pressing C for some reasons, you can resume the previous download with option -c.

`wget -c `


## Download files from Linux command line using curl

Like wget, [curl](https://curl.haxx.se/?ref=itsfoss.com) is also one of the most popular commands to download files in Linux terminal. There are so many ways to [use curl extensively](https://linuxhandbook.com/curl-command-examples/?ref=itsfoss.com) but I’ll focus on only the simple downloading here.

### Installing curl

Though curl doesn’t come preinstalled, it is available in the official repositories of most distributions. You can use your distribution’s package manager to install it.

To [install curl on Ubuntu](https://itsfoss.com/install-curl-ubuntu/) and other Debian based distributions, use the following command:

`sudo apt install curl`


### Download files or webpage using curl

If you use curl without any option with a URL, it will read the file and print it on the terminal screen.

To download a file using curl command in Linux terminal, you’ll have to use the -O (uppercase O) option:

`curl -O URL`


![Download Files In Linux Using Curl](https://itsfoss.com/content/images/wordpress/2020/10/download-files-in-linux-using-curl.png)

It is simpler to download multiple files in Linux with curl. You just have to specify multiple URLs:

`curl -O URL1 URL2 URL3`


Keep in mind that curl is not as simple as wget. While wget saves webpages as index.html, curl will complain of remote file not having a name for webpages. You’ll have to save it with a custom name as described in the next section.

### Download files with a different name

It could be confusing but to provide a custom name for the downloaded file (instead of the original source name), you’ll have to use -o (lowercase O) option:

`curl -o filename URL`


![Download Files In Linux Command line using curl](https://itsfoss.com/content/images/wordpress/2020/10/download-files-in-linux-using-curl-1.png)

Some times, curl wouldn’t just download the file as you expect it to. You’ll have to use option -L (for location) to download it correctly. This is because some times the links redirect to some other link and with option -L, it follows the final link.

### Pause and resume download with curl

Like wget, you can also resume a paused download using curl with option -c:

`curl -C URL`


**Conclusion**

As always, there are multiple ways to do the same thing in Linux. Downloading files from the terminal is no different.

wget and curl are just two of the most popular commands for downloading files in Linux. There are more such command line tools. [Terminal based web-browsers](https://itsfoss.com/terminal-web-browsers/) like [elinks](http://elinks.or.cz/?ref=itsfoss.com), [w3m](http://w3m.sourceforge.net/?ref=itsfoss.com) etc can also be used for downloading files in command line.

Personally, for a simple download, I prefer using wget over curl. It is simpler and less confusing because you may have a difficult time figuring out why curl could not download a file in the expected format.

Your feedback and suggestions are welcome.