What is a curl command? It is a command available on most . It is an abbreviation for “Client URL”. Curl commands are designed to work as a way to check connectivity to URLs and as a great tool for transferring data. So let’s learn how you can start using it.
The Curl command supports the following list of protocols:
- HTTP and HTTPS
- FTP and FTPS
- IMAP and IMAPS
- POP3 and POP3S
- SMB and SMBS
- SFTP
- scp
- TELNET
- GOPHER
- LDAP and LDAPS
- SMTP and SMTP
These are the most important supported protocols, but there are a few others as well. Curl works with libcurl, which is a free client-side URL parsing library.
Save up to 70% on fast and secure Linux VPS hosting. Take advantage of the offers at !
Check the version of Curl
As with any , before we start working with Curl, we need to log into our VPS. If you need help, check out this tutorial on .
First, let’s check which version of Curl is available with the following command:
curl –version
The output will show the Curl version with a list of supported protocols. Now we can see some examples of Curl commands.
Basic Curl Command Syntax
Let’s learn how to use Curl commands. The basic Curl syntax looks like this:
curl
The simplest use of Curl is to display the content of a page. The following example will display the home page of testdomain.com.
curl testdomain.com
This will generate the full source code for the domain’s home page. If no protocol is specified, curl will interpret it as HTTP.
Curl Script Options
Curl commands can download files from a remote location. You can do it in two different ways:
- -EITHER will save the file in the current working directory with the same filename as the remote.
- -either allows you to specify a different file name or location.
Here is an example:
curl -O http://testdomain.com/testfile.tar.gz
The above command will save this as testfile.tar.gz.
curl -o newtestfile.tar.gz http://testdomain.com/testfile.tar.gz
The above command will save this as newtestfile.tar.gz.
If for some reason the download is interrupted, you can resume it using curl. You can do it with the following command:
curl -C – -O http://testdomain.com/testfile.tar.gz
Using curl, we can also download multiple files, as shown below:
curl -O http://testdomain.com/testfile.tar.gz -O http://mydomain.com/myfile.tar.gz
If you want to download multiple files from multiple URLs, include them all in one file. Curl commands can be combined with xargs to download the different URLs.
For example, if we have a file allUrls.txt containing a list of all URLs to be downloaded, the following example can be used to download all files.
xargs –n 1 curl -O < allUrls.txt
Curl Commands for Http
Curl can also be used when there is a proxy server. If you are behind a proxy server listening on port 8090 at sampleproxy.com, download the files as shown below:
curl -x sampleproxy.com:8090 -U username:password -O http:// testdomain.com/testfile.tar.gz
In the example above, you can omit -U username:password if the proxy does not require an authentication method.
A typical HTTP request will always contain a header. The HTTP header sends additional information about the remote web server along with the actual request. While you can check the header information through a browser’s developer tools, you can check it using a curl command.
Below is an example of how to retrieve header information from a website.
curl -I www.testdomain.com
Using curl, you can make a request GET and one POST. An application GET it will be like this:
curl http://mydomain.com
A sample of an application POST it will be like this:
curl –data “text=Hello” https://myDomain.com/firstPage.jsp
Here text=hello is the POST request parameter. This behavior would be similar to HTML forms.
You can also specify multiple HTTP methods in a single curl command. Do it using the option –nextSo:
curl –data “text=Hello” https://myDomain.com/firstPage.jsp –next https://myDomain.com/displayResult.jsp
This contains a POST request followed by a GET request.
Every HTTP request will have a user agent that is sent as part of the request. This indicates the details of the client’s web browser. By default, a curl request contains curl and the version number as the user agent details. An output of this would look like this:
“GET / HTTP/1.1” 200 “_” ”curl/7/29/0”
You can change this default user agent information using the following command:
curl -I http://mydomain.com –user-agent “My new Browser”
Now the modified result will be:
“GET / HTTP/1.1” 200 “_” ”My new Browser”
Cookie Curl
Curl commands can be used to check which cookies are downloaded at any URL. So if you’re accessing https://www.samplewebsite.comyou can cast to a file, save the cookies, and access them using cat or a VM editor.
The following is a sample of such a command:
curl –cookie-jar Mycookies.txt https://www.samplewebsite.com /index.html -O
Similarly, if you have the cookies in a file, you can send it to the website. An example of such a command is shown below:
curl –cookie Mycookies.txt https://www. samplewebsite.com
Curl for FTP
The Curl command supports FTP! You can use it to download files from a remote server.
curl -u username:password -O ftp://sampleftpserver/testfile.tar.gz
In the above command, ftp://sampleftpserver it is an FTP server that accepts connections. The username and password can be omitted for anonymous FTP connections. Type the command and watch the progress bar progress.
You can also upload files with the following command:
curl -u username:password -T testfile.tar.gz ftp://sampleftpserver
Again, we can omit the username and password for anonymous FTP connections.
Limit Curl Output
While using Curl, you can’t tell how big the output will be. You can restrict the bandwidth to make sure it’s not throttled by curl.
The following command restricts the bandwidth to 100K:
curl –limit-rate 100K http://testdomain.com/samplefile.tar.gz -O
conclusion
Curl is a powerful and widely used command. It is useful when you depend on the command line. It has several options and supports multiple protocols. That’s a very good reason to learn this command!
Remember, if you want to learn some advanced commands, just refer to the manual which should be in every version of Unix:
man curl
We hope this tutorial has given you a good starting point for using Curl! How will you use this command? Let us know in the comments!
Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.