Web browsers aren’t the only Linux programs that visit websites. The programs curl and wget can download web pages and other web content with a single command, without touching a browser. By default, curl prints its output to stdout, and wget saves its output to a file (after printing lots of diagnostic messages).
Syntax
The syntax of the curl commands is:
# curl [options] {URL}
curl Command Examples
1. Download the contents of a URL to a file:
# curl http://example.com --output filename
2. Download a file, saving the output under the filename indicated by the URL:
# curl --remote-name http://example.com/filename
3. Download a file, following location redirects, and automatically continuing (resuming) a previous file transfer and return an error on server error:
# curl --fail --remote-name --location --continue-at - http://example.com/filename
4. Send form-encoded data (POST request of type `application/x-www-form-urlencoded`). Use `–data @file_name` or `–data @’-‘` to read from STDIN:
# curl --data 'name=bob' http://example.com/form
5. Send a request with an extra header, using a custom HTTP method:
# curl --header 'X-My-Header: 123' --request PUT http://example.com
6. Send data in JSON format, specifying the appropriate content-type header:
# curl --data '{"name":"bob"}' --header 'Content-Type: application/json' http://example.com/users/1234
7. Pass a username and password for server authentication:
# curl --user myusername:mypassword http://example.com
8. Pass client certificate and key for a resource, skipping certificate validation:
# curl --cert client.pem --key key.pem --insecure https://example.com
Difference between curl and wget
While wget and curl perform the same basic function, there are some key differences:
- wget is a command-line utility only, whereas curl is implemented using the cross-platform libcurl library and is therefore more easily ported to other systems.
- wget can download files recursively, whereas curl cannot.
- curl supports many more network protocols than wget, which only supports HTTP/S and FTP.
- wget is better suited for straightforward downloading of files from a web server, whereas curl is better suited to building and managing more complex requests and responses from web servers.