In this article, I will show you how to use the curl command in Linux with 15 practical examples. The curl command is one of the most powerful and useful commands used by web developers /PHP programmers/ System Admins. It is mainly used to transfer data with URLs between remote to the local system. Using this command in the terminal we can access or download the content from a website, API’s testing, troubleshoot networks related issues, upload files and post data to the website without having a browser. cURL is an inbuilt command line tool and it will be available on most of the Linux/Unix based systems. Even Microsoft announced, that curl becomes as one of their default components of Windows with the version (Windows 10 build 17063 version).
The cURL stands for Client URL Request Library. It is a fully open-sourced package and completely free to use on all the crossed platforms. It supports various Internet Protocols like DICT, FILE, FTP, FTPS, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTSP, SCP, SFTP, SMTP and SMTPS. I am sure, most of the IT champions are very familiar with this curl command.
This guide will help you how to use various options with curl commands. All the below examples are tested on RHEL/CENTOS 7.6
The Global Syntax of the curl command:
curl [options] [URL…]
1. How to find the version of curl?
# curl -V OR # curl –version
Note: If your system has a curl package, it will display the information about curl and the libcurl version with the system architecture and also it will list all the supporting Internet Protocols of libcurl.
2. How use a basic syntax of cURL into Terminal?
# curl linuxteck.com
Note: The curl command will simply fetch the content of the given URL and it will display it onto the Terminal. If we don’t provide the specific protocol with curl, then it will be assumed and take the default one. In the above example, you can see it printed the HTML page of linuxteck.com, where it was taken the default protocol of HTTP.
3. How to download a file?
# curl -O https://www.linuxteck.com/idf_wddevents19sep19_sql.sql.gz
# curl -o backup.sql.gz https://www.linuxteck.com/idf_wddevents19sep19_sql.sql.gz
Note: Here we can use curl with uppercase and lowercase ‘-O and -o’ options to download a file.
The ‘curl -O’ option will save the file name the same as in the URL only
The ‘curl -o’ option can choose a different name to save the output file
In the above example, I have used ‘curl -O’ to save the file with the same name as in the URL “idf_wddevents19sep19_sql.sql.gz” and using ‘curl -o’ to save the output file as “backup.sql.gz”.
4. How to download multiple files?
# curl -O https://www.linuxteck.com/idf-fullbackup19sep19.tar.gz -O https://www.linuxteck.com/idf_wddevents19sep19_sql.sql.gz
Note: Using the above curl command we can download multiple files at a time. In the above example, I have downloaded 2 different files from the same website “idf-backup19sep19.tar.gz and idf_wddevents19sep19_sql.sql.gz” at a time with the advantage of using ‘-O’ option. You can try with different websites.
5. How to continue from an interrupted download?
curl -C – -O https://www.linuxteck.com/idf_wddevents19sep19_sql.sql.gz
Note: Using the above command we can easily resume the file at the point of interruption. For e.g., If we started to download a big sized file and in-between when it gets interrupted, then we need to start the process once again from the beginning. To avoid the process again from the beginning, we can use the above command to continue where the download got interrupted.
6. How to limit the download speed rate?
# curl –limit-rate 1m -O https://www.linuxteck.com/idf_wddevents19sep19_sql.sql.gz
Note: The ‘–limit-rate’ option can control the bandwidth, it can be used to limit the data transfer rate to either 100K or 1M or 1G (K-Kilobytes, M-Megabytes, and G-Gigabytes). In the above example, I have used 1M bandwidth to limit the download speed rate.
7. How to display the download status in a progress bar?
# curl -# -O https://www.linuxteck.com/idf_wddevents19sep19_sql.sql.gz
Note: The above command will display the data transfer progress rate using a single straight “bar” instead of a curl’s standard meter, like, the size of data, transfer rates and times, etc. There is no particular advantage of using this progress bar, just the look is simple and interesting to see the status of the downloading progress than the default meter.
8. How to download files from FTP server?
The Normal FTP:
# curl -u FTP_UserName:FTP_Password -O ftp://linuxteck.com/README.txt
For SFTP :
# curl -u SFTP_UserName:SFTP_Password -O -k sftp://linuxteck.com/README.txt
Note: Using the above commands we can download files directly from FTP/SFTP server. To download the file from the FTP server you need to use the 1st command i.e, “The Normal FTP:” stated above and for the SFTP you can use the 2nd command stated above “For SFTP:”. The ‘-k’ flag used in SFTP is explicitly allowed curl to perform “insecure” SSL connections and transfers. You can also download files using with anonymous login.
In the above example, I have downloaded the sample file named “README.txt” from both FTP and SFTP Servers. If you want only to list down all the files in your Terminal, then you can use the above commands without the ‘-O’ option.
9. How to upload files to FTP server?
The Normal FTP:
# curl -u FTP_UserName:FTP_Password -T linuxteck.sql.gz ftp://linuxteck.com
For SFTP :
# curl -u SFTP_UserName:SFTP_Password -T -k sftp://linuxteck.com/README.txt
Note: Using the above commands we can upload files directly to FTP/SFTP server. To upload the file to the FTP server you need to use the 1st command i.e, “The Normal FTP:” stated above and for the SFTP you can use the 2nd command stated above “For SFTP:”. The ‘-T’ flag represents to upload-file and the ‘-k’ flag used in SFTP is explicitly allowed curl to perform “insecure” SSL connections and transfers. Here also you can use an anonymous login to upload the files. In the above example, I have uploaded directly “linuxteck.sql.gz” file to both the FTP and SFTP Server.
10. How to delete files directly from FTP Server?
# curl ftp://linuxteck.com -X ‘DELE linuxteck.sql.gz’ -u FTP_UserName:FTP_Password
Note: Using the above command we can delete files directly from the FTP Server, but make sure and be clear before executing this command. In this example, I have deleted “linuxteck.sql.gz” file using the above command.
11. How to send an email using SMTP Protocol?
# curl –url “smtps://smtp.example.com:465” –ssl-reqd –mail-from “[email protected]” –mail-rcpt “[email protected]” –upload-file /root/linuxteck.com/emailcontent.txt –user “[email protected]:Password” –insecure
Note: Using the above curl command we can send emails directly to recipient email using SMTP protocol in Terminal. For testing, you need to replace all the “example.com” domain with your respective domain name. Please find the short explanation of the above command :
a. smtps: Use the SMTP address and the respective port of your domain.
b. — mail-from: From Address (person who is sending email).
c. –mail-rcpt: To Address ( Recipient Address).
d. — upload-file: A text file to compose your email (In the above example I used the file name is “emailcontent.txt”).
e. — user: This is your SMTP [email protected]: password
f. — insecure: This is nothing but just to skip the unknown SSL certificates
12. How to follow HTTP Redirect?
# curl -L google.com
Note: Using the ‘curl -L’ option will help you to get the response to the redirected website. For instance, the abc.com site was redirected to xyz.com and if someone hit ‘curl abc.com’ in the Terminal, then it will popup an error like “301 Moved” OR “301 Moved Permanently” OR “The document has moved”.
To get rid of this error, we can add ‘-L’ flag, then it will be redirected to the xyz.com and get the response. For testing, you can use the google.com or any redirected site with and without the ‘-L’ option to understand.
13. How to download a file which is modified before/after a particular time?
curl -z 2-Jul-19 https://www.linuxteck.com/abc.html
curl -z -2-Jul-19 https://www.linuxteck.com/abc.html
Note: Using with ‘-z’ option we can download files from websites that are modified either before or after a specific time mentioned with the above command. In the above examples, I have added two commands (After & Before). For instance, if the file abc.html was modified after 2-Jul-19, then you can use the 1st command stated above downloading the file and if it was modified before 2-Jul-19, then you can use the 2nd the command stated above.
The minus (-) sign in front of the date field is to find and download the file if it was modified just before the time mentioned. That’s it.
14. How to get a definition of the word using the DICT protocol?
# curl dict://dict.org/d:computer
Note: The output is pretty lengthy, hence I pasted only the first few lines. Please do the test at your end and see the result/output. The DICT protocol is used to get the definition of words. In curl command, we need to add the” dict protocol + the dictionary URL + word for the definition”. In the above examples, I used ‘dict.org’ is Directory URL and ‘d: computer’ is the word for the meaning. It listed the meaning of the word “Computer”.
There are plenty of dictionaries are available on the Internet, use the following command to list down all in Terminal.
# curl dict://dict.org/show:db
15. How to transfer the data using Proxy?
# curl -x proxy.example.com:3128 -O https://www.linuxteck.com/abc.html
Note: Using the’-x’ option with the IP address OR FQDN with the port of the proxy server can download/transfer content from a website. In the above example, you can see the file “abc.html” has been downloaded using the proxyserver:3128 with port no of 3128.
I hope this article will help you to learn ‘curl’ commands with examples. Drop me your feedback/comments. If you like this article, kindly share it ?, so that it may help others as well.