Curl is a command-line utility that allows you to transfer data from or to a server in Linux or macOS. It is widely used for transferring data from servers to client applications and vice versa, making it an essential tool for any developer or system administrator.
In this blog post, we will look at 11 curl command examples that you can use in your Linux environment.
What is the “curl” command in Linux?
As per the man page:
curl is a tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET, and TFTP). The command is designed to work without user interaction.
In simple terms, curl downloads files from servers. Even it can transfer files to the servers.
11 curl Command Examples in Unix/Linux
1. Test local webserver or website is working or not
To test the web server or website, you can directly use curl with the domain name like below:
curl localhost curl www.google.com
2. Download a file from the web
To download a file from the web using curl, you can use the following command:
curl -O https://example.com/path/to/file.txt
This will download the file “file.txt” from “https://example.com/path/to/” and save it in the current working directory.
3. Send a POST request
To send a POST request using curl, you can use the following command:
curl -X POST -d "key1=value1&key2=value2" http://example.com/path/to/resource
This will send a POST request to “http://example.com/path/to/resource” with the data “key1=value1&key2=value2” in the body of the request.
4. Download a file from a password-protected site
To download a file from a password-protected site using curl, you can use the following command:
curl -u username:password -O https://example.com/path/to/file.txt
This will download the file “file.txt” from “https://example.com/path/to/” using the specified username and password.
5. Follow redirects
To follow redirects using curl, you can use the “-L” flag:
curl -L https://example.com/path/to/resource
This will follow any redirects that may be present in the response from the server.
6. Display the response headers
To display the response headers using curl, you can use the “-i” flag:
curl -i https://example.com/path/to/resource
This will display the response headers along with the response body.
7. Save the response to a file
To save the response to a file using curl, you can use the “> filename” syntax:
curl https://example.com/path/to/resource > file.txt
This will save the response to the file “file.txt”.
8.Set a custom user agent
To set a custom user agent using curl, you can use the “-A” flag:
curl -A "Custom User Agent" https://example.com/path/to/resource
This will send a request with the specified user agent string.
9. Send a request with a custom header
To send a request with a custom header using curl, you can use the “-H” flag:
curl -H "X-Custom-Header: value" https://example.com/path/to/resource
This will send a request with the specified custom header.
10. Send a request with basic authentication
To send a request with basic authentication using curl, you can use the “-u” flag:
curl -u username:password https://example.com
11. Uploading a file using curl command
To upload a file using curl, you can use the -X POST flag to specify the POST method and the -F flag to specify the file you want to upload.
curl -X POST -F "file=@"
For example, to upload the file example.txt to the server, you can use the following command:
curl -X POST -F "[email protected]" https://example.com/upload
These are 11 examples of the “curl” command that you can use to simplify your day-to-day work as a system engineer, Linux administrator, data engineer, or in any other role.
If you want to add any other helpful example, please comment below.