curl command in Linux with Examples

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 file at once.

Syntax:

curl [options] [URL...]

URL : The most basic uses 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 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 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

  • –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

    Example:

  • -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 a already existing FTP file you can use the -a or –append option.

  • –libcurl :This option is very useful from a developers 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 the 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:root
    

    Output:

Note: There are a number of other options provided by cURL which can be checked on the man 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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.