curl is a command line tool used to transfer data to/from a server. The tool supports various protocols such as : DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP.
Examples of Curl command:
Of the more than 100 command line options Curl offers, here, in this tutorial, we will cover some of the basic yet useful ones. All the examples are tested on Ubuntu 16.04 and curl version 7.50.3
Example:1 Download and store output in a file
Using the tool, you can download data represented by a URL and store that in a file. Here’s how you can use the tool to download a URL data:
$ curl [url]
For example:
$ curl http://www.cricbuzz.com
The above command will display the downloaded data as output on your terminal. To store the output in a file, run the following command:
$ curl [url] > [name-of -output-file]
$ curl http://www.cricbuzz.com > cricbuzz.html
Keep in mind that Curl will display a progress meter on the terminal regardless of how it is being executed.
Here is the output of above command:
Alternatively, you can also use the -o option to save the output (downloaded data) to a specific file.
$ curl -o [name-of-output-file] [url-name]
For example:
$ curl -o cricbuzz.html http://www.cricbuzz.com
Similarly, there also exists a -O option (‘O’ in caps) that lets you save the downloaded data in a file with name same as that of the remote file.
$ curl -O [url]
For example:
$ curl -O https://curl.haxx.se/docs/manpage.html
The above command will save the downloaded data in a file named ‘manpage.html’.
Example:2 Silent Curl command output
If you don’t want Curl to display progress details and errors in output, then you can use the -s option.
For example, consider the following case in which Curl throws an error:
$ curl https://lti.com
To mute errors like these, use the -s option.
$ curl -s [url]
For example:
$ curl -s https://lti.com
Here is the output:
As you can see, no error is shown in the output.
In case you want Curl to only display errors and not any other details (like the progress details it displays by default), use the -S option along with the -s option.
For example:
$ curl -s -S https://www.lti.com
Example:3 Download multiple files
Using the tool, you can download multiple files through a single command.
$ curl -o/O [url1] -o/O [url2]
For example:
$ curl -O https://curl.haxx.se/docs/manpage.html -O https://curl.haxx.se/docs/manual.html
Needless to say, when you will use the ‘-o’ option, you’ll have to provide a file name in the command to store the output.
Example:4 Handle URL redirects using curl command
Suppose, you provide a URL to the Curl command, but the web page doesn’t exist (say, it has been moved to some other location). In that case, you can use the -L command line option, which will make curl redo the request on the new place.
For example, consider a case where-in Curl gives an error like ‘page moved‘.
But if you access the web page through a Web browser, you observe a redirect. Now, to make sure Curl also handles this redirect, use the -L command line option.
$ curl -L [url]
For example:
$ curl -L uber.com
Here is the output:
Example:5 Information about the url using the -v/–trace option
If you want, you can retrieve detailed information about a Curl operation. This feature can be accessed using the -v option.
Lines starting with ‘>‘ and ‘<‘ display the header data which is sent and received respectively by Curl, and * means additional information provided by the tool.
$ curl -v [url]
For example:
$ curl -v https://curl.haxx.se/docs/manpage.html
Here is the output:
If you are not satisfied with the details you got using the -v option and want to access more information, then use the –trace option.
$ curl –trace [file-in-which-you-want-to-store-output] [url]
For example:
$ curl --trace info https://curl.haxx.se/docs/manpage.html
Here is the output:
Read Also : 12 Useful wget Command Practical Examples in Linux
Example:6 Lookup word-meaning using DICT protocol with curl command
Using the tool, you can search for a word on the terminal using dict protocol. A dictionary server dict.org url is passed to it. There are around 77 dictionaries supported by dict.org server.
To list all the supported dictionaries on your terminal, run the following command:
$ curl dict://dict.org/show:db
Here is the output:
Now, to search a word in a specific dictionary, use the following command:
$ curl dict://dict.org/d:[word-to-be-searched]:[dictionary-name]
For example:
$ curl dict://dict.org/d:command:gcide
Note: gcide is a short name for a dictionary named “The Collaborative International Dictionary of English”.
Here is the output:
If you want search a word in all the dictionaries, run the following command:
$ curl dict://dict.org/d:[word-to-be-searched]:*
For example:
$ curl dict://dict.org/d:command:*
Conclusion
We’ve just scratched the surface here as Curl offers a bucket load of features that you can access through the various command line options. Try out the examples explained in this tutorial, and wait for the second part of this tutorial series.