cURL 与 wget：你应该选用哪一个？
curl wget 下载
![](/data/attachment/album/201802/10/113214z991g8h9g99zfd4h.jpg)


当想要直接通过 Linux 命令行下载文件，马上就能想到两个工具：wget 和 cURL。它们有很多一样的特征，可以很轻易的完成一些相同的任务。


虽然它们有一些相似的特征，但它们并不是完全一样。这两个程序适用与不同的场合，在特定场合下，都拥有各自的特性。


### cURL vs wget： 相似之处


wget 和 cURL 都可以下载内容。它们的核心就是这么设计的。它们都可以向互联网发送请求并返回请求项。这可以是文件、图片或者是其他诸如网站的原始 HTML 之类。


这两个程序都可以进行 HTTP POST 请求。这意味着它们都可以向网站发送数据，比如说填充表单什么的。


由于这两者都是命令行工具，它们都被设计成可脚本化。wget 和 cURL 都可以写进你的 [Bash 脚本](https://www.maketecheasier.com/beginners-guide-scripting-linux/) ，自动与新内容交互，下载所需内容。


### wget 的优势


![wget download](/data/attachment/album/201802/10/113216xpktn68tnvwnd1hw.jpg "wget download")


wget 简单直接。这意味着你能享受它超凡的下载速度。wget 是一个独立的程序，无需额外的资源库，更不会做其范畴之外的事情。


wget 是专业的直接下载程序，支持递归下载。同时，它也允许你下载网页中或是 FTP 目录中的任何内容。


wget 拥有智能的默认设置。它规定了很多在常规浏览器里的事物处理方式，比如 cookies 和重定向，这都不需要额外的配置。可以说，wget 简直就是无需说明，开罐即食！


### cURL 优势


![cURL Download](/data/attachment/album/201802/10/113216zp8zar8l1tgpbuku.jpg "cURL Download")


cURL是一个多功能工具。当然，它可以下载网络内容，但同时它也能做更多别的事情。


cURL 技术支持库是：libcurl。这就意味着你可以基于 cURL 编写整个程序，允许你基于 libcurl 库中编写图形环境的下载程序，访问它所有的功能。


cURL 宽泛的网络协议支持可能是其最大的卖点。cURL 支持访问 HTTP 和 HTTPS 协议，能够处理 FTP 传输。它支持 LDAP 协议，甚至支持 Samba 分享。实际上，你还可以用 cURL 收发邮件。


cURL 也有一些简洁的安全特性。cURL 支持安装许多 SSL/TLS 库，也支持通过网络代理访问，包括 SOCKS。这意味着，你可以越过 Tor 来使用cURL。


cURL 同样支持让数据发送变得更容易的 gzip 压缩技术。


### 思考总结


那你应该使用 cURL 还是使用 wget？这个比较得看实际用途。如果你想快速下载并且没有担心参数标识的需求，那你应该使用轻便有效的 wget。如果你想做一些更复杂的使用，直觉告诉你，你应该选择 cRUL。


cURL 支持你做很多事情。你可以把 cURL 想象成一个精简的命令行网页浏览器。它支持几乎你能想到的所有协议，可以交互访问几乎所有在线内容。唯一和浏览器不同的是，cURL 不会渲染接收到的相应信息。




---


via: <https://www.maketecheasier.com/curl-vs-wget/>


作者：[Nick Congleton](https://www.maketecheasier.com/author/nickcongleton/) 译者：[CYLeft](https://github.com/CYLeft) 校对：[wxy](https://github.com/wxy)


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


OK
![Curl Command Guide Featured](https://www.maketecheasier.com/assets/uploads/2021/11/curl-command-guide-featured.jpg)

If you’ve been following terminal-focused installation instructions for Linux applications for a while, you’ve probably come across the `curl`

command at some point or another. cURL is a command-line tool for transferring data with URLs. One of the simplest uses is to download a file via the command line. This is deceptive, however, as cURL is an incredibly powerful tool that can do much more.

**Also read:** [How to Copy and Paste Text, Files and Folders in Linux Terminal](https://www.maketecheasier.com/copy-paste-files-linux-command-line/)

## What Is cURL?

Originally written by Daniel Sternberg in 1996 to grab financial data from web servers and broadcast it to IRC channels, cURL has evolved to become a powerful tool for getting data without having to use a browser. If you’re always using the terminal, this will be one of the more important tools in your arsenal.

In most Linux distributions, cURL is preinstalled in the system, and you can use it straight away. Even if it is not installed, it is also found in most repositories, so you can easily install it using the Software Center.

For Windows, it does not have a “curl-like” command, and macOS has cURL preinstalled but doesn’t offer [quite as many flags](https://ss64.com/osx/curl.html) as the Linux version.

**Also read:** [How to Use the dd Command in Linux](https://www.maketecheasier.com/use-dd-command-linux/)

## Installation

Before we proceed any further, we have to make sure that cURL is already installed on our system.

### Linux

In Debian/Ubuntu-based distros, use the following command to install cURL:

sudo apt install curl

In Arch-based distros:

sudo pacman -S curl

In Fedora/CentOS/RHEL:

sudo dnf install curl

### macOS

For macOS, it is already preinstalled, so you don’t need to do anything.

**Also read:** [How to Launch Terminal in the Current Folder Location on Mac](https://www.maketecheasier.com/launch-terminal-current-folder-mac/)

### Windows

- For Windows 7/10/11, head over to the
[cURL download page](https://curl.se/windows/)and choose from either the 64-bit or 32-bit packages, according to the architecture you’re running. If you don’t know your architecture, 64-bit is a safe bet, as the vast majority of hardware made after 2006 is on it. - Create a folder either directly on the system drive or in “C:\Program Files\” and call it “cURL.”

![Linuxcurl Newfolder](https://www.maketecheasier.com/assets/uploads/2021/11/linuxcurl-newfolder.png)

- Go back to the zip file you downloaded, open it, and find “curl.exe” inside the “bin” folder. Copy that to the cURL folder you created. The EXE you copied is completely self-contained and capable of running every permutation you can run on Linux.
- To make this command actually useful, we have to add it to the PATH variable in Windows so that it can run from the command prompt anywhere.

- Click on your Start menu, type
`environment`

, and press`Enter`. - Click on “Environment Variables … ” You should now be in your environment variables settings.

![Linuxcurl Environmentvariables](https://www.maketecheasier.com/assets/uploads/2021/11/linuxcurl-environmentvariables.png)

- Select the “Path” environment variable, then click “Edit … “
- Once in the path edit dialog window, click “New” and type out the directory where your “curl.exe” is located – for example, “C:\Program Files\cURL”.

![Linuxcurl Editvariable](https://www.maketecheasier.com/assets/uploads/2021/11/linuxcurl-editvariable.png)

- Click “OK” on the dialog windows you opened through this process and enjoy having cURL in your terminal!

Every flag in cURL that’s usable in Linux should work in the Windows version.

**Word to the wise:** remember that the command prompt should never be confused with Windows Terminal. Windows Terminal comes with its own version of cURL included in the PowerShell that serves similar functionality but works entirely differently.

**Also read:** [How to Use Rm Command in Linux](https://www.maketecheasier.com/use-rm-command-linux/)

## Using cURL

To get started, simply type `curl maketecheasier.com`

in your terminal and press `Enter`.

If you are not getting any output, it is because this site’s server is not configured to respond to random connection requests to its non-www domain. If you polled a server that doesn’t exist or isn’t online, you’d get an error message saying that cURL could not resolve the host.

![Linuxcurl Error](https://www.maketecheasier.com/assets/uploads/2021/10/linuxcurl-error.png)

To get cURL to do something actually useful, we’ll have to specify a protocol. In our example, we are using the HTTPS protocol to query this site’s home page. Type `curl https://www.maketecheasier.com`

.

![Linuxcurl Files](https://www.maketecheasier.com/assets/uploads/2021/10/linuxcurl-files.png)

If all goes well, you should be staring at a gigantic wall of data. To make that data a bit more usable, we can tell cURL to put it into an HTML file:

curl https://www.maketecheasier.com > ~/Downloads/mte.html

![Linuxcurl Download](https://www.maketecheasier.com/assets/uploads/2021/10/linuxcurl-download.png)

This command puts the contents of our site’s output into an HTML file in your Downloads folder. Navigate to the folder with your favorite file manager and double-click the file you just made. It should open a snapshot of the HTML output of this site’s home page.

![Linuxcurl Testing](https://www.maketecheasier.com/assets/uploads/2021/10/linuxcurl-testing.png)

Similarly, you can use the `-o`

flag to achieve the same result:

curl -o ~/Downloads/mte.html https://www.maketecheasier.com

## Following Redirects

Most sites automatically redirect the traffic from “http” to “https” protocol. In cURL, you can achieve the same thing with the `-L`

flag. This will automatically follow 301 redirects until it reaches a readable page or file.

curl -L http://google.com.

![Linuxcurl Redirect](https://www.maketecheasier.com/assets/uploads/2021/11/linuxcurl-redirect.png)

## Resuming a Download

When downloading large files, depending on your Internet speed, interruptions can be immensely irritating. Thankfully, cURL has a resume function. Passing the `-C`

flag will take care of this issue in a jiffy.

To show a real-world example, I interrupted a download of Debian’s testing release ISO on purpose by pressing `Ctrl` and `C` in the middle of grabbing it.

![Linuxcurl Interrupted](https://www.maketecheasier.com/assets/uploads/2021/11/linuxcurl-interrupted.png)

For our next command, are attching the `-C`

flag. For example,

curl -C - -o ~/Downloads/debiantesting.iso -L https://cdimage.debian.org/cdimage/weekly-builds/amd64/iso-dvd/debian-testing-amd64-DVD-1.iso

![Linuxcurl Resume](https://www.maketecheasier.com/assets/uploads/2021/10/linuxcurl-resume.png)

The download successfully started where it left off.

## Downloading More than One File

Because cURL doesn’t have the most intuitive way to download multiple files, there are two methods, each one with its own compromise.

If the files you’re downloading are enumerated (e.g., file1, file2, and so on), you can use brackets to get the full range of files and “#” within the output you specify with the `-o`

flag. To make this a bit less confusing, here’s an example:

curl "http://example.com/file[1-5].zip" -o "#1_#2"

A simpler way of doing this is with `-O`

(`--remote-name`

). This flag makes cURL download the remote file onto a local file of the same name. Since you don’t have to specify an output, you should use this command when the terminal is open in the directory you want to download files to.

curl -O "https://example.com/file1.zip" -O "https://example.com/file2.zip"

If you have a large amount of enumerated files to download, `--remote-name-all`

is a better flag for this:

curl --remote-name-all "https://example.com/file[1-5].zip"

You could even specify non-enumerated files coming from the same site without having to re-type the URL using brackets:

curl --remote-name-all "https://example.com/{file1.zip,anotherfile.zip,thisisfun.zip}"

## Downloading with Authentication

Download files that require authentication (for example, when grabbing from a private FTP server) with the `-u`

flag. Every authentication request must be done with the username first and the password second, with a colon separating the two. Here’s an example to make things simple:

curl -u username:password -o ~/Downloads/file.zip ftp://example.com/file.zip

This will authenticate our friend bonobo_bob into the FTP server and download the file into the Downloads folder.

## Splitting and Merging Files

If for any reason you wish to download a large file and split it into chunks, you can do so with cURL’s `--range`

flag. With `--range`

, you must specify the byte you want to start at up to the one you want to finish at. If you do not specify an end to the range, it will just download the rest of the file.

In the command below, cURL will download the first 100 MB of Arch Linux’s installation image:

curl --range 0-99999999 -o arch.part1 -L https://mirrors.chroot.ro/archlinux/iso/2021.11.01/archlinux-2021.11.01-x86_64.iso

For the next 100 MB, use `--range 100000000-199999999`

, etc. You can chain these commands by using the `&&`

operand:

curl --range 0-99999999 -o arch.part1 -L https://mirrors.chroot.ro/archlinux/iso/2021.11.01/archlinux-2021.11.01-x86_64.iso && \ curl --range 100000000-199999999 -o arch.part2 -L https://mirrors.chroot.ro/archlinux/iso/2021.11.01/archlinux-2021.11.01-x86_64.iso && \ curl --range 200000000-299999999 -o arch.part3 -L https://mirrors.chroot.ro/archlinux/iso/2021.11.01/archlinux-2021.11.01-x86_64.iso && \ curl --range 300000000-399999999 -o arch.part4 -L https://mirrors.chroot.ro/archlinux/iso/2021.11.01/archlinux-2021.11.01-x86_64.iso && \ curl --range 400000000-499999999 -o arch.part5 -L https://mirrors.chroot.ro/archlinux/iso/2021.11.01/archlinux-2021.11.01-x86_64.iso && \ curl --range 500000000-599999999 -o arch.part6 -L https://mirrors.chroot.ro/archlinux/iso/2021.11.01/archlinux-2021.11.01-x86_64.iso && \ curl --range 600000000-699999999 -o arch.part7 -L https://mirrors.chroot.ro/archlinux/iso/2021.11.01/archlinux-2021.11.01-x86_64.iso && \ curl --range 700000000- -o arch.part8 -L https://mirrors.chroot.ro/archlinux/iso/2021.11.01/archlinux-2021.11.01-x86_64.iso

If you followed the above command structure to the letter, eight files should appear where you asked cURL to download them.

![Linuxcurl Parts](https://www.maketecheasier.com/assets/uploads/2021/11/linuxcurl-parts.png)

To reunite these files, you’ll have to use the `cat`

command if you’re on Linux or macOS like so:

cat arch.part? > arch.iso

For Windows, you’ll have to use the `copy`

command like this:

copy /b arch.part* arch.iso

## Other Useful Features

There are plenty of flags and uses for cURL:

`-#`

– Uses a progress bar to indicate how far along you are in what you’re grabbing. Example:`curl -# https://asite.com/somefile.zip > ~/somefile.zip`

.`-a`

– Asks cURL to append to a file rather than overwrite it. Example:`curl -ao ~/collab-full.x https://example-url.com/collab-part26.x`

.`--head`

– Only grabs the response header from the server without the output data. This is useful when you’re either debugging a website or having a peek at the server’s programmed responses to clients. Example:`curl --head https://example-url.com`

.`--limit-rate`

– Orders a download with limited bandwidth. It’s useful in situations where you don’t want cURL hogging all the available bandwidth in your system. A simple number will be interpreted as bytes per second. K represents kilobytes per second; M represents megabytes per second. Example:`curl --limit-rate 8M https://example-url.com/file.zip > ~/file.zip`

.`-o`

– As mentioned earlier, determines an output file for cURL to use. Example:`curl -o ~/Downloads/file.zip https://thefileplace.com/file.zip -o file2.zip https://thefileplace.com/file2.zip`

.`--proxy`

– If you want to work with a proxy, this is the way to do it. Example:`curl --proxy proxyurl:port https://example-url.com/file.zip > ~file.zip`

.

## cURL vs. Wget

Both released in the same year (1996), cURL and [Wget](https://www.maketecheasier.com/use-wget-download-websites-to-pc/) are pretty much sister programs to the casual observer. Dive a little deeper, though, and you can see these two sisters have different purposes.

### Wget

- It’s fully built from the ground up to grab data from the Internet.
- Doesn’t need the
`-L`

or`-o`

flags like cURL; just type`wget [url]`

and go! - Can download recursively to grab everything in a directory with the
`-r`

flag. - Has all the functions a user needs for daily use and caters to everyday sysadmin tasks.
- (In Linux) Doesn’t need many dependencies; all of them should be available out of the box.

**Also read:** [How to Install and Use wget on Mac](https://www.maketecheasier.com/install-wget-mac/)

### cURL

- Expansive repertoire of flags and useful functions for remote retrieval.
- Supports local networking (LDAP) and network printers (Samba).
- Works well with gzip compression libraries.
- Depends on libcurl, which allows developers to write software or bash scripts that include cURL’s functionality.

In short, Wget is the “everyman’s toolbox” for grabbing stuff from the Internet, while cURL expands on this with more granulated control for power users and system administrators.

## Frequently Asked Questions

### 1. I got a certificate error in Linux. How do I fix it?

If you got an error that says something like “peer’s certificate issuer has been marked as not trusted,” the easiest way to fix this is by reinstalling the common certificates package in your distro.

For Debian/Ubuntu-based systems:

sudo apt reinstall ca-certificates

For Fedora/CentOS/RHEL:

`dnf reinstall ca-certificates`

For Arch-based systems:

`pacman -S ca-certificates`

Note that in Arch you may want to clear your package cache using `pacman -Scc`

before reinstalling the certificates package.

If you still get this error, there may be something wrong on the server’s end.

### 2. Is it safe to run cURL and bash commands together?

While not the most common way to install Linux applications, there are a number of developers (such as the people behind NodeJS) that give you no choice but to use `curl`

alongside a root-access command that runs through bash (e.g., `curl [argument] | sudo -E bash -`

) to install the software.

It may look a bit scary, but if the people behind the application are trustworthy, it’s very unlikely you’ll break something. Malicious actors are everywhere and can infiltrate repositories like Arch’s AUR, so installing using `curl`

in combination with root access commands isn’t generally more unsafe than doing so through your package manager.

### 3. Can I use cURL with Tor?

Yes! Start Tor Browser (or a standalone tor service) and use the `--proxy`

flag. Tor gives you a local proxy you can use to mask your IP in other applications. Here’s an example of cURL used with Tor:

curl --proxy localhost:9050 http://example.com

Standalone Tor services will use 9050 as their listening port, while the Tor browser uses port 9150.

## Wrapping Up

cURL has proven resilient amid the changing fabric of the Linux world, keeping its position as an important tool in the terminal user’s arsenal.

If you are new to the command line, check out some of the [most useful Linux commands](https://www.maketecheasier.com/useful-linux-commands/). If you simply want to search the Web instead of downloading data from the Internet, you can [browse on the terminal too](https://www.maketecheasier.com/how-to-search-web-from-linux-terminal/).

Our latest tutorials delivered straight to your inbox