Topics we will cover hide
Introduction to curl command
How to install curl
Syntax to use curl command
Different examples to use curl command
Conclusion
What's Next
Further Reading
Introduction to curl command
curl is a popular command that helps to transfer data from or to a remote server. It supports many protocols including HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP, and FILE.
curl offers useful features like file transfer resume, proxy support, FTP upload, user authentication, POST method, bandwidth limit, and many more. curl is powered by libcurl library for all transfer-related features.
How to install curl
curl is already available in newer versions of Linux distributions. In case, if you get curl command not found
, you can easily install it using the system's package manager tool.
Install curl on CentOS, RHEL, Fedora
$ sudo dnf install curl
Install curl on Ubuntu and Debian
$ sudo apt install curl
Syntax to use curl command
The basic syntax for curl command is:
$ curl [option] url
Different examples to use curl command
1. Show content of the URL
When the URL is specified to the curl command, it displays the source code (HTML content) of that URL.
$ curl https://linux.die.net
Sample Output:
2. Retrieve only HTTP headers of the URL
The -I
or --head
option tells curl to fetch the HTTP headers only.
$ curl -I https://linux.die.net
OR
$ curl --head https://linux.die.net
Sample Output:
3. Download and save the content to file
The output of the curl command can be saved using the -o
or --output
option. This option also requires the file name parameter.
$ curl -o filename linux.die.net
OR
$ curl --output filename linux.die.net
Sample Output:
The following example saves the source code of https://linux.die.net
in a docpage.html
file.
curl displays the progress meter which shows transfer speed, file size, time elapsed, etc.
4. Download a file with curl
If the URL represents a file, you can download and save it as a new name using the -o
or --output
option.
In this example, the filename on the website is ping-website.png
which we will save as ping.png
in our system.
$ curl -o ping.png https://www.golinuxcloud.com/wp-content/uploads/ping-website.png
OR
$ curl --output ping.png https://www.golinuxcloud.com/wp-content/uploads/ping-website.png
Sample Output:
5. Download a file with the same name
If you want to save the file with the original name used on the website, you can use the -O
or --remote-name
option.
$ curl -O https://www.golinuxcloud.com/wp-content/uploads/ping-website.png
OR
$ curl --remote-name https://www.golinuxcloud.com/wp-content/uploads/ping-website.png
Sample Output:
6. Download multiple files
You can specify -o
or -O
option as many times as the number of URLs you have. This helps you to download multiple files with the same command.
$ curl -O url1 -O url2
Sample Output:
The following example downloads both image files ping-count.png
and ping-localhost
with the same name in the current directory.
golinux@ubuntu-PC:~$ curl -O https://www.golinuxcloud.com/wp-content/uploads/ping-count.png -O https://www.golinuxcloud.com/wp-content/uploads/ping-localhost.png % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed100 18338 100 18338 0 0 33524 0 --:--:-- --:--:-- --:--:-- 33524 % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed100 17018 100 17018 0 0 180k 0 --:--:-- --:--:-- --:--:-- 180k
7. Resume the download
If a download is interrupted or stopped, it can be resumed using the -C
or --continue-at
option. When curl is used with -C -
option, it automatically finds out where to resume the transfer.
$ curl -C - -O https://releases.ubuntu.com/22.04.1/ubuntu-22.04.1-desktop-amd64.iso
OR
$ curl --continue-at - -O https://releases.ubuntu.com/22.04.1/ubuntu-22.04.1-desktop-amd64.iso
Sample Output:
Here, we are resuming the download of Ubuntu 22.04 iso file.
8. Follow redirects with curl
By default, curl does not follow the redirects. If the requested page is moved to a different location, we can use curl with -L
or --location
option to make curl follow a redirect.
$ curl -L linux.die.net
OR
$ curl --location linux.die.net
Sample Output:
9. Run curl in silent mode
The -s
or --silent
option tells curl to hide the progress meter or error messages.
$ curl -s linux.die.net
OR
$ curl --silent linux.die.net
Sample Output:
10. curl command to check HTTP Status
You can use this curl command to check any URL connectivity. If it returns HTTP/2 200
, it means the request has succeeded.
$ curl -IsL https://linux.die.net | grep HTTP
Sample Output:
golinux@ubuntu-PC:~$ curl -IsL https://linux.die.net | grep HTTPHTTP/2 200
11. Set the maximum transfer rate
The --limit-rate
option lets you specify the maximum transfer rate. The given value will be taken as bytes/second if a suffix is not used.
You can use kilobytes with k
suffix, megabytes with m
suffix, and gigabytes with g
suffix.
The following command downloads the Ubuntu 22.04 iso file with a limited speed of 2 MB.
$ curl --limit-rate 2m -O https://releases.ubuntu.com/22.04.1/ubuntu-22.04.1-desktop-amd64.iso
Sample Output:
This is helpful if you want to limit the speed and not use the entire bandwidth.
12. Download files from authenticated FTP servers
The -u
or --user
option allows you to specify the username and password to use for server authentication. You can use this syntax to download a file from the password-protected FTP server.
$ curl -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/file.txt
OR
$ curl --user FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/file.txt
13. Upload files to FTP servers
You can use the -T
or --upload-file
option to upload a file to the FTP server. This option requires you to specify the local file you want to upload.
$ curl -T filename -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/file.txt
OR
$ curl --upload-file filename -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/file.txt
14. Send POST requests with curl
Using the -d
or --data
option, you can send data in a POST request to the specified website. The data must be specified in the field=value
pair. You can send multiple data by separating them with a &
character.
This command makes a POST request to https://www.example.com/post
that has fname
and lname
fields in the form.
$ curl -d "fname=John&lname=Doe" https://www.example.com/post
OR
$ curl --data "fname=John&lname=Doe" https://www.example.com/post
15. Use a specific proxy
The -x
or --proxy
option allows you to use a specified HTTP proxy. If you do not specify the port, port number 1080 will be used.
$ curl -x proxy_host:port url
OR
$ curl --proxy proxy_host:port url
16. Specify username and password for proxy authentication
If the proxy server is secured, you can use the -U
or --proxy-user
option to pass the user name and password value as shown below.
$ curl -U username:password -x proxy_host:port url
OR
$ curl --proxy-user username:password -x proxy_host:port url
17. Using curl with DICT protocol
curl supports DICT, a dictionary network protocol. It is helpful for finding the meaning or definition of the word.
For instance, to view the meaning of word machine
, you can use this curl command.
$ curl dict://dict.org/d:machine
Sample Output:
Conclusion
In this tutorial, we have discussed the most common examples of curl commands in Linux. curl is a powerful tool that has a wide range of options. It can come in handy when downloading files and troubleshooting connectivity issues from the command line.
If you have any confusion regarding any examples, please let us know in the comment section.
What's Next
Perform a POST Request Using Curl [Practical Examples]
15+ hping3 command examples in Linux [Cheat Sheet]