Science and technology

Use the web from the command line with curl

Curl is usually thought of a non-interactive internet browser. That means it is capable of pull info from the web and show it in your terminal or put it aside to a file. This is actually what internet browsers, resembling Firefox or Chromium, do besides they render the data by default, whereas curl downloads and shows uncooked info. In actuality, the curl command does far more and has the flexibility to switch knowledge to or from a server utilizing considered one of many supported protocols, together with HTTP, FTP, SFTP, IMAP, POP3, LDAP, SMB, SMTP, and plenty of extra. It’s a great tool for the common terminal consumer, an important comfort for the sysadmin, and a top quality assurance software for microservices and cloud builders.

Curl is designed to work with out consumer interplay, so in contrast to Firefox, you need to take into consideration your interplay with on-line knowledge from begin to end. For occasion, if you wish to view an online web page in Firefox, you launch a Firefox window. After Firefox is open, you kind the web site you wish to go to into the URL area or a search engine. Then you navigate to the positioning and click on on the web page you wish to see.

The identical ideas apply to twist, besides you do it abruptly: you launch curl on the identical time you feed it the web location you need and inform it whether or not you wish to the info to be saved in your terminal or to a file. The complexity will increase when you need to work together with a website that requires authentication or with an API, however when you study the curl command syntax, it turns into second nature. To assist you to get the cling of it, we collected the pertinent syntax info in a helpful cheat sheet.

Download a file with curl

You can obtain a file with the curl command by offering a hyperlink to a particular URL. If you present a URL that defaults to index.html, then the index web page is downloaded, and the file you downloaded is displayed in your terminal display screen. You can pipe the output to much less or tail or another command:

$ curl "http://example.com" | tail -n four
    <h1>Example Domain</h1>
    <p>This area is for use in illustrative examples in paperwork. You might use this area in literature with out prior coordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More info...</a></p>
</div></physique></html>

Because some URLs comprise particular characters that your shell usually interprets, it is most secure to encompass your URL in citation marks.

Some recordsdata do not translate nicely to being displayed in a terminal. You can use the –remote-name choice to trigger the file to be saved in line with what it is known as on the server:

$ curl --remote-name "https://example.com/linux-distro.iso"
$ ls
linux-distro.iso

Alternatively, you need to use the –output choice to call your obtain no matter you need:

curl "http://example.com/foo.html" --output bar.html

List contents of a distant listing with curl

Because curl is non-interactive, it is tough to browse a web page for downloadable components. Provided that the distant server you are connecting to permits it, you need to use curl to listing the contents of a listing:

$ curl --list-only "https://example.com/foo/"

Continue a partial obtain

If you are downloading a really massive file, you may discover that you need to interrupt the obtain. Curl is clever sufficient to find out the place you left off and proceed the obtain. That means the subsequent time you are downloading a 4GB Linux distribution ISO and one thing goes improper, you by no means have to return to the beginning. The syntax for –continue-at is slightly uncommon: if you realize the byte depend the place your obtain was interrupted, you possibly can present it; in any other case, you need to use a lone sprint () to inform curl to detect it mechanically:

$ curl --remote-name --continue-at - "https://example.com/linux-distro.iso"

Download a sequence of recordsdata

If you have to obtain a number of recordsdata—slightly than only one large file—curl can assist with that. Assuming you realize the placement and file-name sample of the recordsdata you wish to obtain, you need to use curl’s sequencing notation: the beginning and finish level between a spread of integers, in brackets. For the output filename, use #1 to point the primary variable:

$ curl "https://example.com/file_[1-4].webp" --output "file_#1.webp"

If you have to use one other variable to signify one other sequence, denote every variable within the order it seems within the command. For instance, on this command, #1 refers back to the directories images_00zero by means of images_009, whereas #2 refers back to the recordsdata file_1.webp by means of file_4.webp:

$ curl "https://example.com/images_00[0-9]/file_[1-4].webp"
--output "file_#1-#2.webp"

Download all PNG recordsdata from a website

You can do some rudimentary internet scraping to seek out what you wish to obtain, too, utilizing solely curl and grep. For occasion, say you have to obtain all photographs related to an online web page you are archiving. First, obtain the web page referencing the photographs. Pipe the web page to grep with a seek for the picture kind you are concentrating on (PNG on this instance). Finally, create a whereas loop to assemble a obtain URL and to avoid wasting the recordsdata to your pc:

$ curl https://instance.com |
grep --only-matching 'src="http://opensource.com/[^"]*.[png]"' |
lower -d" -f2 |
whereas learn i; do
curl https://instance.com/"$" -o "$";
executed

This is simply an instance, however it demonstrates how versatile curl will be when mixed with a Unix pipe and a few intelligent, however primary, parsing.

Protocols used for knowledge trade have loads of metadata embedded within the packets that computer systems ship to speak. HTTP headers are parts of the preliminary portion of knowledge. It will be useful to view these headers (particularly the response code) when troubleshooting your connection to a website:

curl --head "https://example.com"
HTTP/2 200
accept-ranges: bytes
age: 485487
cache-control: max-age=604800
content-type: textual content/html; charset=UTF-eight
date: Sun, 26 Apr 2020 09:02:09 GMT
etag: "3147526947"
expires: Sun, 03 May 2020 09:02:09 GMT
last-modified: Thu, 17 Oct 2019 07:18:26 GMT
server: ECS (sjc/4E76)
x-cache: HIT
content-length: 1256

Fail rapidly

A 200 response is the same old HTTP indicator of success, so it is what you often count on whenever you contact a server. The well-known 404 response signifies web page cannot be discovered, and 500 means there was a server error.

To see what errors are occurring throughout negotiation, add the –show-error flag:

$ curl --head --show-error "http://opensource.ga"

These will be tough so that you can repair except you could have entry to the server you are contacting, however curl typically tries its greatest to resolve the placement you level it to. Sometimes when testing issues over a community, seemingly countless retries simply waste time, so you possibly can power curl to exit upon failure rapidly with the –fail-early choice:

curl --fail-early "http://opensource.ga"

Redirect question as specified by a 3xx response

The 300 collection of responses, nevertheless, are extra versatile. Specifically, the 301 response implies that a URL has been moved completely to a distinct location. It’s a typical means for an internet site admin to relocate content material whereas leaving a “trail” so folks visiting the previous location can nonetheless discover it. Curl does not observe a 301 redirect by default, however you may make it proceed on to a 301 vacation spot by utilizing the –location choice:

$ curl "https://iana.org" | grep title
<title>301 Moved Permanently</title>
$ curl --location "https://iana.org"
<title>Internet Assigned Numbers Authority</title>

Expand a shortened URL

The –location choice is helpful whenever you wish to take a look at shortened URLs earlier than visiting them. Shortened URLs will be helpful for social networks with character limits (after all, this is probably not a problem in the event you use a modern and open source social network) or for print media through which customers cannot simply copy and paste a protracted URL. However, they will also be slightly harmful as a result of their vacation spot is, by nature, hid. By combining the –head choice to view simply the HTTP headers and the –location choice to unravel the ultimate vacation spot of a URL, you possibly can peek right into a shortened URL with out loading the total useful resource:

$ curl --head --location
"https://bit.ly/2yDyS4T"

Once you observe fascinated by the method of exploring the net as a single command, curl turns into a quick and environment friendly approach to pull the data you want from the web with out bothering with a graphical interface. To assist you to construct it into your normal workflow, we have created a cheat sheet with frequent curl makes use of and syntax, together with an outline of utilizing it to question an API.

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Most Popular

breakingExpress.com features the latest multimedia technologies, from live video streaming to audio packages to searchable archives of news features and background information. The site is updated continuously throughout the day.

Copyright © 2017 Breaking Express, Green Media Corporation

To Top