Skip to content
Related Articles

Related Articles

curl command in Linux with Examples

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 10 Mar, 2022
Improve Article
Save Article

curl is a command-line tool to transfer data to or from a server, using any of the supported protocols (HTTP, FTP, IMAP, POP3, SCP, SFTP, SMTP, TFTP, TELNET, LDAP, or FILE). curl is powered by Libcurl. This tool is preferred for automation since it is designed to work without user interaction. curl can transfer multiple files at once. 
Syntax:  

curl [options] [URL...]

URL: The most basic use of curl is typing the command followed by the URL.  

curl https://www.geeksforgeeks.org

This should display the content of the URL on the terminal. The URL syntax is protocol dependent and multiple URLs can be written as sets like: 

curl http://site.{one, two, three}.com

URLs with numeric sequence series can be written as: 

curl ftp://ftp.example.com/file[1-20].jpeg

Progress Meter: curl displays a progress meter during use to indicate the transfer rate, amount of data transferred, time left, etc. 

curl -# -O ftp://ftp.example.com/file.zip
curl --silent ftp://ftp.example.com/file.zip

If you like a progress bar instead of a meter, you can use the -# option as in the example above, or –silent if you want to disable it completely. 

Example:

Options: 

-o: saves the downloaded file on the local machine with the name provided in the parameters. 
Syntax:

curl -o [file_name] [URL...]
  • Example:
curl -o hello.zip ftp://speedtest.tele2.net/1MB.zip
  • Output:

  • The above example downloads the file from the FTP server and saves it with the name hello.zip.

-O: This option downloads the file and saves it with the same name as in the URL. 
Syntax:

curl -O [URL...]
  • Example:
curl -O ftp://speedtest.tele2.net/1MB.zip
  • Output:

-C -: This option resumes download which has been stopped due to some reason. This is useful when downloading large files and was interrupted. 
Syntax:

curl -C - [URL...]
  • Example:
curl -C - -O ftp://speedtest.tele2.net/1MB.zip
  • Output:

–limit-rate: This option limits the upper bound of the rate of data transfer and keeps it around the given value in bytes. 
Syntax:

curl --limit-rate [value] [URL]
  • Example:
curl --limit-rate 1000K -O ftp://speedtest.tele2.net/1MB.zip
  • Output:

  • The command limits the download to 1000K bytes.

-u: curl also provides options to download files from user authenticated FTP servers. 
Syntax:

curl -u {username}:{password} [FTP_URL]
  • Example:
curl -u demo:password -O ftp://test.rebex.net/readme.txt
  • Output: 

-T: This option helps to upload a file to the FTP server. 
Syntax:

curl -u {username}:{password} -T {filename} {FTP_Location}
  • If you want to append an already existing FTP file you can use the -a or –append option.

–libcurl: This option is very useful from a developer’s perspective. If this option is appended to any cURL command, it outputs the C source code that uses libcurl for the specified option. It is a code similar to the command line implementation. 
Syntax: 

curl [URL...] --libcurl [filename]
  • Example:
curl https://www.geeksforgeeks.org > log.html --libcurl code.c
  • Output:

  • The above example downloads the HTML and saves it into log.html and the code in code.c file. The next command shows the first 30 lines of the code.

-x, –proxy: curl also lets us use a proxy to access the URL. 
Syntax:

curl -x [proxy_name]:[port] [URL...]
  • If the proxy requires authentication, it can be used with the command: 
curl -u [user]:[password] -x [proxy_name]:[port] [URL...]

Sending mail: As curl can transfer data over different protocols, including SMTP, we can use curl to send mails. 
Syntax: 

curl –url [SMTP URL] –mail-from [sender_mail] –mail-rcpt [receiver_mail] -n –ssl-reqd -u {email}:{password} -T [Mail text file] 

DICT protocol: The Libcurl defines the DICT protocol which can be used to easily get the definition or meaning of any word directly from the command line. 
Syntax: 

curl [protocol:[dictionary_URL]:[word]
  • Example:
curl dict://dict.org/d:overclock
  • Output:

Note: There are a number of other options provided by cURL which can be checked on the main page. The libcurl library has been ported into various programming languages. It’s advisable to visit the individual project site for documentation.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!