15 Practical Linux cURL Command Examples (cURL Download Examples)
15 Practical Linux cURL Command Examples (cURL Download Examples)
To store the output in a file, you an redirect it as shown below. This will also display some additional download statistics.
$ curl http://www.centos.org > centos-org.html % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 27329 0 27329 0 0 104k 0 --:--:-- --:--:-- --:--:-- 167k
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.
% Total 66 1215k 100 1215k % Received % Xferd 66 805k 100 1215k 0 0 0 0 Average Speed Time Dload Upload Total 33060 0 0:00:37 39474 0 0:00:31 Time Time Current Spent Left Speed 0:00:24 0:00:13 45900 0:00:31 --:--:-- 68987
When you use curl -O (uppercase O), it will save the content in the file named gettext.html itself in the local machine.
$ curl -O http://www.gnu.org/software/gettext/manual/gettext.html
Note: 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. Similar to cURL, you can also use wget to download files. Refer to wget examples to understand how to use wget effectively.
The below command will download both index.html and gettext.html and save it in the same name under the current directory.
$ 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.
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.ins html source code.
$ curl -L http://www.google.com
thegeekstuff.com//curl-examples/
1/4
11/04/2012
interrupted.
If we say -C -, then curl will find from where to start resuming the download. We can also give an offset -C <offset>. The given offset bytes will be skipped from the beginning for the source file. Start a big download using curl, and press Ctrl-C to stop it in between the download.
$ curl -O http://www.gnu.org/software/gettext/manual/gettext.html ############## 20.1%
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%.
curl -C - -O http://www.gnu.org/software/gettext/manual/gettext.html ############### 21.1%
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.
% Total 1 1215k 1 1215k 1 1215k % Received % Xferd 1 13601 1 14601 1 15601 0 0 0 0 0 0 Average Speed Time Dload Upload Total 957 0 0:21:40 960 0 0:21:36 962 0 0:21:34 Time Spent 0:00:14 0:00:15 0:00:16 Time Current Left Speed 0:21:26 999 0:21:21 999 0:21:18 999
The above command will download the yy.html only if it is modified later than the given date and time
$ 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
Note: By default curl uses Basic HTTP Authentication. We can specify other authentication method using ntlm | digest.
The above command will download the xss.php file from the ftp server and save it in the local directory.
$ 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.
The above command will list out all the packages from a-z ranges in the terminal.
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.
thegeekstuff.com//curl-examples/
2/4
11/04/2012
Optionally we can use . to get the input from STDIN and transfer to the remote.
$ 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.
HTTP 1.0, assume close after body HTTP/1.0 200 OK Date: Tue, 10 Apr 2012 11:18:39 GMT Expires: -1 Cache-Control: private, max-age=0 Content-Type: text/html; charset=ISO-8859-1 Set-Cookie: PREF=ID=7c497a6b15cc092d:FF=0:TM=1334056719:LM=1334056719:S=UORpBwxFmTRkbXLj; expires=Thu, 10-Apr-2014 11:18:39 GMT; path=/; do
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
=> Send header, 169 bytes (0xa9) 0000: 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 31 0d 0a GET / HTTP/1.1.. 0010: 55 73 65 72 2d 41 67 65 6e 74 3a 20 63 75 72 6c User-Agent: curl .. 0060: 2e 32 2e 33 2e 34 20 6c 69 62 69 64 6e 2f 31 2e .2.3.4 libidn/1. 0070: 31 35 20 6c 69 62 73 73 68 32 2f 31 2e 0080: 0d 0a 48 6f 73 74 3a 20 77 77 77 2e 67 0090: 6c 65 2e 63 6f 2e 69 6e 0d 0a 41 63 63 00a0: 3a 20 2a 2f 2a 0d 0a 0d 0a == Info: HTTP 1.0, assume close after body <= Recv header, 17 bytes (0x11) 0000: 48 54 54 50 2f 31 2e 30 20 32 30 30 20 0010: 0a 32 2e 36 15 libssh2/1.2.6 6f 6f 67 ..Host: www.goog 65 70 74 le.co.in..Accept : */*....
This verbose and trace option will come in handy when curl fails due to some reason and we dont know why.
The above command will list the meaning for bash as follows
151 "Bash" gcide "The Collaborative International Dictionary of English v.0.48" Bash \Bash\, v. t. [imp. & p. p. {Bashed}; p. pr. & vb. n. {Bashing}.] [Perh. of imitative origin; or cf. Dan. baske to strike, bask a blow, Sw. basa to beat, bas a beating.] To strike heavily; to beat; to crush. [Prov. Eng. & Scot.] --Hall Caine. [1913 Webster] Bash her open with a rock. [Webster 1913 Suppl.] --Kipling.
. 151 "Bash" gcide "The Collaborative International Dictionary of English v.0.48" Bash \Bash\, n. 1. a forceful blow, especially one that does damage to its target. [PJC] . .
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
$ curl dict://dict.org/show:db jargon "The Jargon File (version 4.4.7, 29 Dec 2003)" foldoc "The Free On-line Dictionary of Computing (26 July 2010)" easton "Easton's 1897 Bible Dictionary" hitchcock "Hitchcock's Bible Names Dictionary (late 1800's)" bouvier "Bouvier's Law Dictionary, Revised 6th Ed (1856)"
Now in-order to find the actual meaning of Bash in computer we can search for bash in foldoc dictionary as follows
thegeekstuff.com//curl-examples/
3/4
11/04/2012
$ curl dict://dict.org/d:bash:foldoc
Once the above command is entered, it will wait for the user to provide the data to mail. Once youve composed your message, type . (period) as the last line, which will send the email immediately.
Subject: Testing This is a test mail .
thegeekstuff.com//curl-examples/
4/4