Download a Single File
The following command will get the content of the URL and display it in the STDOUT (i.e on your terminal).
1 | $ curl http://www.centos.org |
To store the output in a file, you an redirect it as shown below. This will also display some additional download statistics.
1 | $ curl http://www.centos.org > centos-org.html |
Save the cURL Output to a file
We can save the result of the curl command to a file by using -o/-O options.
- o (lowercase o) the result will be saved in the filename provided in the command line
- O (uppercase O) the filename in the URL will be taken and it will be used as the filename to store the result
1 | $ curl -o mygettext.html http://www.gnu.org/software/gettext/manual/gettext.html |
Now the page gettext.html will be saved in the file named ‘mygettext.html’. You can also note that when running curl with -o option, it displays the progress meter for the download as follows.
1 | % Total % Received % Xferd Average Speed Time Time Time Current |
When you use curl -O (uppercase O), it will save the content in the file named ‘gettext.html’ itself in the local machine.
1 | $ curl -O http://www.gnu.org/software/gettext/manual/gettext.html |
When curl has to write the data to the terminal, it disables the Progress Meter, to avoid confusion in printing. We can use ‘>’|’-o’|’-O’ options to move the result to a file.
Fetch Multiple Files at a time
We can download multiple files in a single shot by specifying the URLs on the command line.
Syntax:
1 | $ curl -O URL1 -O URL2 |
The below command will download both index.html and gettext.html and save it in the same name under the current directory.
1 | $ curl -O http://www.gnu.org/software/gettext/manual/html_node/index.html -O http://www.gnu.org/software/gettext/manual/gettext.html |
Please note that when we download multiple files from a same sever as shown above, curl will try to re-use the connection.
Follow HTTP Location Headers with -L option
By default CURL doesn’t follow the HTTP Location headers. It is also termed as Redirects. When a requested web page is moved to another place, then an HTTP Location header will be sent as a Response and it will have where the actual web page is located.
For example, when someone types google.com in the browser from India, it will be automatically redirected to ‘google.co.in’. This is done based on the HTTP Location header as shown below.
1 | $ curl http://www.google.com |
The above output says that the requested document is moved to ‘http://www.google.co.in/’.
We can insists curl to follow the redirection using -L option, as shown below. Now it will download the google.co.in’s html source code.
1 | $ curl -L http://www.google.com |
Continue/Resume a Previous Download
Using curl -C option, you can continue a download which was stopped already for some reason. This will be helpful when you download large files, and the download got interrupted.
If we say ‘-C -‘, then curl will find from where to start resuming the download. We can also give an offset ‘-C
Start a big download using curl, and press Ctrl-C to stop it in between the download.
1 | $ curl -O http://www.gnu.org/software/gettext/manual/gettext.html |
Note: -# is used to display a progress bar instead of a progress meter.
Now the above download was stopped at 20.1%. Using “curl -C -“, we can continue the download from where it left off earlier. Now the download continues from 20.1%.
1 | curl -C - -O http://www.gnu.org/software/gettext/manual/gettext.html |
Limit the Rate of Data Transfer
You can limit the amount at which the data gets transferred using –limit-rate option. You can specify the maximum transfer rate as argument.
1 | $ curl --limit-rate 1000B -O http://www.gnu.org/software/gettext/manual/gettext.html |
The above command is limiting the data transfer to 1000 Bytes/second. curl may use higher transfer rate for short span of time. But on an average, it will come around to 1000B/second.
The following was the progress meter for the above command. You can see that the current speed is near to the 1000 Bytes.
1 | % Total % Received % Xferd Average Speed Time Time Time Current |
Download a file only if it is modified before/after the given time
We can get the files that are modified after a particular time using -z option in curl. This will work for both FTP & HTTP.
1 | $ curl -z 21-Dec-11 http://www.example.com/yy.html |
The above command will download the yy.html only if it is modified later than the given date and time
1 | $ curl -z -21-Dec-11 http://www.example.com/yy.html |
The above command will download the yy.html, if it is modified before than the given date and time.
Please refer ‘man curl_getdate’ for the various syntax supported for the date expression
Pass HTTP Authentication in cURL
Sometime, websites will require a username and password to view the content ( can be done with .htaccess file ). With the help of -u option, we can pass those credentials from cURL to the web server as shown below.
1 | $ curl -u username:password URL |
Note: By default curl uses Basic HTTP Authentication. We can specify other authentication method using –ntlm | –digest.
Download Files from FTP server
cURL can also be used to download files from FTP servers. If the given FTP path is a directory, by default it will list the files under the specific directory.
1 | $ curl -u ftpuser:ftppass -O ftp://ftp_server/public_html/xss.php |
The above command will download the xss.php file from the ftp server and save it in the local directory.
1 | $ curl -u ftpuser:ftppass -O ftp://ftp_server/public_html/ |
Here, the given URL refers to a directory. So cURL will list all the files and directories under the given URL
If you are new to FTP/sFTP, refer ftp sftp tutorial for beginners.
List/Download using Ranges
cURL supports ranges to be given in the URL. When a range is given, files matching within the range will be downloaded. It will be helpful to download packages from the FTP mirror sites.
1 | $ curl ftp://ftp.uk.debian.org/debian/pool/main/[a-z]/ |
The above command will list out all the packages from a-z ranges in the terminal.
Upload Files to FTP Server
Curl can also be used to upload files to the FTP server with -T option.
1 | $ curl -u ftpuser:ftppass -T myfile.txt ftp://ftp.testserver.com |
The above command will upload the file named myfile.txt to the FTP server. You can also upload multiple files at a same time using the range operations.
1 | $ curl -u ftpuser:ftppass -T "{file1,file2}" ftp://ftp.testserver.com |
Optionally we can use “.” to get the input from STDIN and transfer to the remote.
1 | $ curl -u ftpuser:ftppass -T - ftp://ftp.testserver.com/myfile_1.txt |
The above command will get the input from the user from Standard Input and save the contents in the ftp server under the name ‘myfile_1.txt’.
You can provide one ‘-T’ for each URL and the pair specifies what to upload where.
More Information using Verbose and Trace Option
You can get to know what is happening using the -v option. -v option enable the verbose mode and it will print the details
1 | curl -v http://google.co.in |
The about command will output the following
1 | * About to connect() to www.google.co.in port 80 (#0) |
If you need more detailed information then you can use the –trace option. The trace option will enable a full trace dump of all incoming/outgoing data to the given file
1 | => Send header, 169 bytes (0xa9) |
1 | 0070: 31 35 20 6c 69 62 73 73 68 32 2f 31 2e 32 2e 36 15 libssh2/1.2.6 |
This verbose and trace option will come in handy when curl fails due to some reason and we don’t know why.
Get Definition of a Word using DICT Protocol
You can use cURL to get the definition for a word with the help of DICT protocol. We need to pass a Dictionary Server URL to it.
1 | $ curl dict://dict.org/d:bash |
The above command will list the meaning for bash as follows
1 | 151 "Bash" gcide "The Collaborative International Dictionary of English v.0.48" |
Now you can see that it uses “The Collaborative International Dictionary of English”. There are many dictionaries are available. We can list all the dictionaries using
1 | $ curl dict://dict.org/show:db |
Now in-order to find the actual meaning of Bash in computer we can search for bash in “foldoc” dictionary as follows
1 | $ curl dict://dict.org/d:bash:foldoc |
The result will be,
1 | bash |
Use Proxy to Download a File
We can specify cURL to use proxy to do the specific operation using -x option. We need to specify the host and port of the proxy.
1 | $ curl -x proxysever.test.com:3128 http://google.co.in |
Send Mail using SMTP Protocol
cURL can also be used to send mail using the SMTP protocol. You should specify the from-address, to-address, and the mailserver ip-address as shown below.1
$ curl --mail-from blah@test.com --mail-rcpt foo@test.com smtp://mailserver.com
Once the above command is entered, it will wait for the user to provide the data to mail. Once you’ve composed your message, type . (period) as the last line, which will send the email immediately.1
2
3Subject: Testing
This is a test mail
.