The cURL command in Linux is a powerful tool for transferring data to or from a server using various protocols, including HTTP, HTTPS, FTP, and more. It is commonly used for automating web requests, testing APIs, and downloading files from the internet. cURL is versatile and can handle a wide range of tasks, from simple file downloads to complex multi-part form submissions.
In this guide, we will explore 30 practical examples of using the cURL command in Linux. These examples will cover common use cases, such as downloading files, sending data to APIs, handling authentication, and more, showcasing the flexibility and power of cURL for various tasks.
The Syntax of cURL Command
Before we proceed into demonstrating 30 examples with the cURL command, its a good idea to understand its syntax first:
curl [options] [URL]
In this syntax:
- options: These are the parameters that modify the behavior of cURL.
- URL: This is the address of the file or resource you want to interact with.
cURL Command Options
cURL offers many options that allow you to customize its behavior. Here are some of the most commonly used options:
- -O: Downloads the file from the specified URL.
- -I: Fetches the HTTP-header from the server.
- -u: Specifies the user credentials for server authentication.
- -d: Sends the specified data in a POST request to the server.
- -x: Specifies the proxy address to use for the requests.
30 Examples of cURL Command in Linux
Now, let’s delve into some practical examples of using the cURL command. Each example will be prefaced with an explanation followed by a demonstration of the output.
Example 1: Fetching Data from a URL
The most basic use of cURL is to fetch the contents of a webpage. Here’s how you can do it:
curl https://www.example.com
This command fetches the HTML content of the webpage at www.example.com.
Example 2: Downloading a File
cURL can also be used to download files from the internet. Here’s an example:
curl -O https://www.example.com/file.txt
This command downloads the file file.txt from www.example.com and saves it in the current directory.
Example 3: Sending a POST Request
You can use cURL to send POST requests to a server. Here’s how:
curl -d "param1=value1¶m2=value2" -X POST http://www.example.com
This command sends an HTTP POST request to www.example.com with the data param1=value1¶m2=value2.
Example 4: Fetching HTTP Headers
If you want to fetch the HTTP headers from a server, you can use the -I option. Here’s an example:
curl -I https://www.example.com
This command fetches the HTTP headers from www.example.com.
Example 5: Using a Proxy
If you need to use a proxy, specify it using the -x option. Here’s how:
curl -x http://proxy.example.com:8080 https://www.example.com
This command sends the request to www.example.com through the proxy at proxy.example.com:8080.
Example 6: Sending Cookies
You can send cookies along with your request using the -b option. Here’s an example:
curl -b "name=value" https://www.example.com
This command sends a cookie with name=value to www.example.com.
Example 7: Sending User Agent
Websites often use the user agent to deliver content suitable for the client’s browser. To send a user agent with your request, use the -A option:
curl -A "Mozilla/5.0" https://www.example.com
This command sends a request to www.example.com with the user agent set as Mozilla/5.0.
Example 8: Following Redirects
Some URLs redirect to other URLs. To follow these redirects, use the -L option:
curl -L https://www.example.com
This command follows any redirects from www.example.com.
Example 9: Saving Output to a File
To save the output of a cURL command to a file, use the -o option:
curl -o output.html https://www.example.com
This command saves the output of www.example.com to output.html.
Example 10: Uploading Files with FTP
cURL can upload files to a server using FTP. Here’s how:
curl -T file.txt ftp://ftp.example.com --user username:password
This command uploads file.txt to ftp.example.com using the provided username and password.
Example 11: Resuming a Download
If a download gets interrupted, you can resume it with the -C – option:
curl -C - -O https://www.example.com/file.txt
This command resumes the download of file.txt from www.example.com.
Example 12: Downloading Multiple Files
To download multiple files, specify multiple URLs:
curl -O https://www.example.com/file1.txt -O https://www.example.com/file2.txt
This command downloads file1.txt and file2.txt from www.example.com.
Example 13: Sending a DELETE Request
To send a DELETE request, use the -X DELETE option:
curl -X DELETE https://www.example.com/resource
This command sends a DELETE request to the URL www.example.com/resource.
Example 14: Verbose Output
For detailed information about the request and response, use the -v option:
curl -v https://www.example.com
This command provides verbose output for the request to www.example.com.
Example 15: Silent Mode
To suppress the progress meter and error messages, use the -s option:
curl -s https://www.example.com
This command fetches the content of www.example.com in silent mode.
Example 16: Displaying the Download Progress
To display the download progress in a more readable format, use the # option:
curl -# -O https://www.example.com/file.txt
This command downloads file.txt from www.example.com and displays the progress as a progress bar.
Example 17: Sending JSON Data
To send JSON data in a POST request, use the -H option to set the content type:
curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST https://www.example.com
This command sends a POST request with JSON data to www.example.com.
Example 18: Using cURL with an API
cURL is often used to interact with APIs. Here’s an example:
curl -H "Authorization: Bearer YOUR_TOKEN" https://api.example.com
This command sends a request to https://api.example.com with an authorization header.
Example 19: Downloading Files in the Background
To download a file in the background, use the -O option with an ampersand at the end:
curl -O https://www.example.com/file.txt &
This command downloads file.txt from www.example.com in the background.
Example 20: Sending Data from a File
To send data from a file in a POST request, use the @ symbol:
curl -d @data.txt -X POST https://www.example.com
This command sends a POST request with the data from data.txt to www.example.com.
Example 21: Fetching Content from FTP Server
cURL can be used to fetch content from an FTP server. Here’s how:
curl ftp://ftp.example.com/file.txt --user username:password
This command fetches file.txt from ftp.example.com using the provided username and password.
Example 22: Fetching Content from a Password-Protected Website
To fetch content from a password-protected website, use the -u option:
curl -u username:password https://www.example.com
This command fetches the content from www.example.com using the provided username and password.
Example 23: Fetching Content from a Website with SSL
To fetch content from a website with SSL, use the -k option:
curl -k https://www.example.com
This command fetches the content from www.example.com, ignoring any SSL certificate warnings.
Example 24: Sending a PUT Request
To send a PUT request, use the -X PUT option:
curl -X PUT -d "data" https://www.example.com/resource
This command sends a PUT request with the data “data” to www.example.com/resource.
Example 25: Fetching the Response Headers
To fetch only the response headers, use the -I option:
curl -I https://www.example.com
This command fetches only the response headers from www.example.com.
Example 26: Fetching Content from a Website with Cookies
To fetch content from a website with cookies, use the -b option:
curl -b cookies.txt https://www.example.com
This command fetches the content from www.example.com using the cookies stored in cookies.txt.
Example 27: Fetching Content from a Website with Custom Headers
To fetch content from a website with custom headers, use the -H option:
curl -H "Custom-Header: Value" https://www.example.com
Example 28: Fetching Content from a Website with a Timeout
To fetch content from a website with a timeout, use the -m option:
curl -m 10 https://www.example.com
This command fetches the content from www.example.com with a timeout of 10 seconds.
Example 29: Fetching Content from a Website in Verbose Mode
To fetch content from a website in verbose mode, use the -v option:
curl -v https://www.example.com
This command fetches the content from www.example.com in verbose mode, displaying detailed information about the request and response.
Example 30: Fetching Content from a Website and Displaying the Progress Meter
To fetch content from a website and display the progress meter, use the -# option:
curl -# https://www.example.com
This command fetches the content from www.example.com and displays the progress meter.
Conclusion
Mastering the cURL command with these practical examples can significantly enhance your ability to interact with web services, automate data transfers, and troubleshoot network issues. Regular practice with different cURL options and commands will deepen your understanding and enable you to leverage its full potential in your Linux workflows. Enjoy the versatility and efficiency that cURL brings to your command-line toolkit.
- How to Install ModSecurity 2 with Apache on Debian 12 or 11 - Tuesday, September 17, 2024
- How to Install Remi RPM on Rocky Linux 9 or 8 - Monday, September 16, 2024
- How to Install Akregator on Ubuntu 24.04, 22.04 or 20.04 - Tuesday, September 3, 2024