Skip to content

Linux curl command

The Linux curl command is a powerful tool used to transfer data to or from a server. It supports various protocols like HTTP, HTTPS, FTP, and more. With its versatile options, you can customize the request headers, follow redirects, save the output to a file, and even send data in different formats. In addition to making basic GET and POST requests, curl can also perform tasks like downloading files, testing APIs, and automating tasks through scripts. Mastering the curl command is essential for any developer or system administrator working in a Linux environment.

curl Syntax:

Terminal window
curl [options] [URL]

Options:

OptionDescription
-X, —requestSpecify the request method (GET, POST, etc.)
-H, —headerAdd a header to the request
-d, —dataSend data in the request body
-o, —outputWrite output to a file
-v, —verboseMake the operation more talkative
-i, —includeInclude the HTTP-header in the output
-A, —user-agentSpecify the User-Agent string

Parameters:

ParameterDescription
URLThe URL to send the request to
dataData to be sent in the request body
methodThe request method to use (GET, POST, PUT, etc.)
fileFile to write output to
headerCustom header to be included in the request

curl Usage:

Download a File from a URL

Terminal window
curl -O https://www.example.com/file.zip

Downloads a file named “file.zip” from the specified URL.

Follow Redirects

Terminal window
curl -L https://www.example.com

Retrieves a page content from a URL and follows any redirects to the final destination.

Save Output to a File

Terminal window
curl -o output.txt https://www.example.com/data

Retrieves data from a URL and saves the output to a file named “output.txt”.

Include HTTP Headers in Output

Terminal window
curl -i https://www.example.com/api

Retrieves the HTTP headers along with the content from the specified URL.

How do I interact with a website using curl in Linux?

To interact with a website using curl in Linux, you can send a GET request to the URL. For example:

Terminal window
curl https://www.example.com

How do I download a file using curl in Linux?

To download a file using curl in Linux, specify the URL of the file with the -o option to save it with a specific name. For example:

Terminal window
curl -o filename.zip https://www.example.com/file.zip

How do I follow redirects with curl in Linux?

To follow redirects during a curl request in Linux, use the -L or --location option. For example:

Terminal window
curl -L https://www.example.com

How do I set a custom user agent with curl in Linux?

To set a custom user agent header in the request using curl in Linux, use the -A or --user-agent option. For example:

Terminal window
curl -A "Custom User Agent" https://www.example.com

How do I send POST data with curl in Linux?

To send POST data using curl in Linux, use the -d or --data option followed by the data to be sent. For example:

Terminal window
curl -d "key1=value1&key2=value2" https://www.example.com/api

How do I download a file with progress information using curl in Linux?

To download a file with progress information displayed in Linux, use the -# or --progress-bar option with curl. For example:

Terminal window
curl -# -o filename.zip https://www.example.com/file.zip

Applications of the curl command

  • Download files from the web
  • Upload files to a server
  • Test API endpoints
  • Retrieve web pages or website content
  • Follow redirects
  • Send form data
  • Perform RESTful operations
  • Transfer data over different protocols like HTTP, HTTPS, FTP, etc.