15 basic curl command in Linux with practical examples

This article explains how to use curl in Linux with 15 examples. The curl command is one of the most powerful and useful tools that are used by web developers as well as by PHP programmers and System Administrators. Most commonly, it is used to transfer data between remote and local systems. Through this command, we can access or download content from a website without a browser. It is also useful for testing API's, troubleshooting network-related issues, uploading files, and posting data to the website. cURL is an inbuilt command line utility that runs on most Linux/Unix-based systems. Microsoft even announced that the curl will become a standard component of Windows with version (Windows 10 build 17063).

CURL stands for Client URL Request Library. This is a fully open-source package that is completely free to use on all 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. The majority of IT champions are probably well-versed in the curl command.

This guide will help you learn how to use various options with curl commands. All the below examples were tested on RHEL/CENTOS 7.6

Global Syntax of the curl command:

curl [options] [URL...]

1. How to find the version of curl?

# curl -V          OR           # curl --version

how to find the version of curl in linuxNote: 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 to use the basic syntax of cURL into Terminal?

# curl linuxteck.com

how to find the basic syntax of curlNote: 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 on the HTML page of linuxteck.com, where the default protocol of HTTP was taken.

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

download files using curlNote: Here you 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

download multiple files using curl commandNote: 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 start to download a large size 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, you 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 the progress bar?

# curl -# -O https://www.linuxteck.com/idf_wddevents19sep19_sql.sql.gz

new progress bar using curl commandNote: 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 rather than the default meter.

8. How to download files from  an 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

download files from ftp server using curl commandNote: 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 explicitly allows 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 only want 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 the upload file and the '-k' flag used in SFTP explicitly allows curl to perform "insecure" SSL connections and transfers. Here you can use an anonymous login to upload the files. In the above example, I have uploaded the "linuxteck.sql.gz" file directly 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

delete files using curl command from ftp serverNote: 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 the "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

send emails using curl command with smtp protocol

Note: Using the above curl command we can send emails directly to the recipient email using SMTP protocol in Terminal. For testing, you need to replace all the "example.com" domains with your respective domain names. 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 skipping 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 a response to the redirected website. For instance, the abc.com site was redirected to xyz.com and if someone hits '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 the '-L' flag, then it will redirect you to xyz.com and get a response. For testing, you can use google.com or any redirecting site with or without the '-L' option to understand.

13. How to download a file which is modified before/after a particular time?

After:

 

curl -z 2-Jul-19 https://www.linuxteck.com/abc.html

 

Before:

 

curl -z -2-Jul-19 https://www.linuxteck.com/abc.html

Note: Using the '-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 data 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

dict protocol using curl command

Note: The output is pretty lengthy, hence I pasted only the first few lines. Please do the test on your end and see the results/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' as the Directory URL and 'd: computer' is the word for the meaning. It listed the meaning of the word "Computer".

There are plenty of dictionaries available on the Internet, use the following command to list down all in Terminal.

# curl dict://dict.org/show:db

dirct protocol using curl command in linux

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 you can download/transfer content from a website. In the above example, you can see that 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.

Thank you!

1 reply on “15 basic curl command in Linux with practical examples”

Leave a Reply

Your email address will not be published. Required fields are marked *

L