Curl Command in Linux with Examples

By 

Updated on

7 min read

Linux Curl Command Examples

curl is a command-line utility for transferring data from or to a server designed to work without user interaction. With curl, you can download or upload data using one of the supported protocols including HTTP, HTTPS, SCP , SFTP , and FTP . curl provides a number of options allowing you to resume transfers, limit bandwidth, use proxies, add user authentication, and much more.

In this guide, we cover curl with real examples and the options you’ll use most often.

For a quick reference, see the Curl cheatsheet .

Installing Curl

The curl package is pre-installed on most Linux distributions today.

To check whether the curl package is installed on your system, open your terminal, type curl, and press Enter. If you have curl installed, the system will print curl: try 'curl --help' or 'curl --manual' for more information. Otherwise, you will see something like curl command not found.

If curl is not installed, you can easily install it using the package manager of your distribution.

Install Curl on Ubuntu and Debian

Terminal
sudo apt update
sudo apt install curl

Install Curl on CentOS and Fedora

Terminal
sudo yum install curl

How to Use Curl

The syntax for the curl command is as follows:

txt
curl [options] [URL...]

In its simplest form, when invoked without any option, curl displays the specified resource to the standard output.

For example, to retrieve the example.com homepage, you would run:

Terminal
curl example.com

The command will print the source code of the example.com homepage in your terminal window.

If no protocol is specified, curl tries to guess the protocol you want to use, and it will default to HTTP.

Save the Output to a File

To save the result of the curl command, use either the -o or -O option.

Lowercase -o saves the file with a predefined filename, which in the example below is vue-v2.6.10.js:

Terminal
curl -o vue-v2.6.10.js https://cdn.jsdelivr.net/npm/vue/dist/vue.js

Uppercase -O saves the file with its original filename:

Terminal
curl -O https://cdn.jsdelivr.net/npm/vue/dist/vue.js

Download Multiple Files

To download multiple files at once, use multiple -O options, followed by the URL to the file you want to download.

In the following example, we’re downloading the Arch Linux and Debian ISO files:

Terminal
curl -O https://geo.mirror.pkgbuild.com/iso/latest/archlinux-x86_64.iso  \
     -O https://www.debian.org/distrib/netinst

Resume a Download

You can resume a download by using the -C - option. This is useful if your connection drops during the download of a large file, and instead of starting the download from scratch, you can continue the previous one.

For example, if you are downloading the Ubuntu 24.04 ISO file using the following command:

Terminal
curl -O https://releases.ubuntu.com/noble/ubuntu-24.04.4-live-server-amd64.iso

and suddenly your connection drops, you can resume the download with:

Terminal
curl -C - -O https://releases.ubuntu.com/noble/ubuntu-24.04.4-live-server-amd64.iso

Get the HTTP Headers of a URL

HTTP headers are colon-separated key-value pairs containing information such as User-Agent, content type, and encoding. Headers are passed between the client and the server with the request or the response.

Use the -I option to fetch only the HTTP headers of the specified resource:

Terminal
curl -I --http2 https://www.ubuntu.com/
curl get http headers

Save the Response Headers to a File

If you want to inspect the headers later, use -D to save them to a file:

Terminal
curl -D headers.txt https://www.ubuntu.com/ -o /dev/null

Test if a Website Supports HTTP/2

To check whether a particular URL supports the new HTTP/2 protocol , fetch the HTTP headers with -I along with the --http2 option:

Terminal
curl -I --http2 -s https://linuxize.com/ | grep HTTP

The -s option tells curl to run in silent mode and hide the progress meter and error messages.

If the remote server supports HTTP/2, curl prints HTTP/2.0 200:

output
HTTP/2 200

Otherwise, the response is HTTP/1.1 200:

output
HTTP/1.1 200 OK

If you have curl version 7.47.0 or newer, you do not need to use the --http2 option because HTTP/2 is enabled by default for all HTTPS connections.

Show Status Code and Timing

To see the status code and basic timing information without printing the response body, use -w with -o /dev/null:

Terminal
curl -sS -o /dev/null -w "status=%{http_code} time=%{time_total}s\n" https://linuxize.com/

Follow Redirects

By default, curl doesn’t follow the HTTP Location headers.

If you try to retrieve the non-www version of google.com, you will notice that instead of getting the source of the page you’ll be redirected to the www version:

Terminal
curl google.com
curl follow redirects

The -L option instructs curl to follow any redirect until it reaches the final destination:

Terminal
curl -L google.com

Change the User-Agent

Sometimes when downloading a file, the remote server may be set to block the curl User-Agent or to return different contents depending on the visitor device and browser.

In situations like this, to emulate a different browser, use the -A option.

For example, to emulate Firefox, you would use:

Terminal
curl -A "Mozilla/5.0 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0" https://getfedora.org/

Specify a Maximum Transfer Rate

The --limit-rate option allows you to limit the data transfer rate. The value can be expressed in bytes, kilobytes with the k suffix, megabytes with the m suffix, and gigabytes with the g suffix.

In the following example, curl will download the Go binary and limit the download speed to 1 MB:

Terminal
curl --limit-rate 1m -O https://go.dev/dl/go1.23.5.linux-amd64.tar.gz

This option is useful to prevent curl from consuming all the available bandwidth.

Transfer Files via FTP

To access a protected FTP server with curl, use the -u option and specify the username and password as shown below:

Terminal
curl -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/

Once logged in, the command lists all files and directories in the user’s home directory.

You can download a single file from the FTP server using the following syntax:

Terminal
curl -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/file.tar.gz

To upload a file to the FTP server, use the -T followed by the name of the file you want to upload:

Terminal
curl -T newfile.tar.gz -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/

Send Cookies

Sometimes you may need to make an HTTP request with specific cookies to access a remote resource or to debug an issue.

By default, when requesting a resource with curl, no cookies are sent or stored.

To send cookies to the server, use the -b switch followed by a filename containing the cookies or a string.

For example, to send a session cookie with your request:

Terminal
curl -b "session_id=abc123" https://httpbin.org/cookies

You can also save cookies from a response and reuse them in subsequent requests:

Terminal
# Save cookies to a file
curl -c cookies.txt https://example.com/login

# Send saved cookies with the next request
curl -b cookies.txt https://example.com/dashboard

Request JSON Data

When you’re working with APIs, set the Accept header and keep the output clean:

Terminal
curl -sS -H "Accept: application/json" https://api.github.com/repos/curl/curl

Send Basic Auth Credentials

If a server requires basic authentication, use -u with a username and password:

Terminal
curl -u user:passwd https://httpbin.org/basic-auth/user/passwd

Using Proxies

curl supports different types of proxies, including HTTP, HTTPS, and SOCKS. To transfer data through a proxy server, use the -x (--proxy) option, followed by the proxy URL.

The following command downloads the specified resource using a proxy on 192.168.44.1 port 8888:

Terminal
curl -x 192.168.44.1:8888 https://www.linux.com/

If the proxy server requires authentication, use the -U (--proxy-user) option followed by the user name and password separated by a colon (user:password):

Terminal
curl -U username:password -x 192.168.44.1:8888 https://www.linux.com/

Send POST Requests

By default, curl sends GET requests. To send data with a POST request, use the -X POST option along with -d to specify the data:

Terminal
curl -X POST -d "username=admin&password=secret" https://httpbin.org/post

To send JSON data, set the Content-Type header:

Terminal
curl -X POST -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"secret"}' \
  https://httpbin.org/post

You can also read data from a file using @:

Terminal
curl -X POST -H "Content-Type: application/json" -d @data.json https://httpbin.org/post

Set Timeouts

To prevent curl from hanging indefinitely, you can set timeouts:

Terminal
# Maximum time for the entire operation (30 seconds)
curl --max-time 30 https://example.com/

# Maximum time to establish a connection (10 seconds)
curl --connect-timeout 10 https://example.com/

# Combine both for robust requests
curl --connect-timeout 10 --max-time 60 https://example.com/

Conclusion

curl is a command-line tool that allows you to transfer data from or to a remote host. It is useful for troubleshooting issues, downloading files, and more.

The examples shown in this tutorial are simple, but demonstrate the most used curl options and are meant to help you understand how the curl command works.

For more information about curl, visit the curl documentation page.

If you have any questions or feedback, feel free to leave a comment.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page