
Linux- curl
kommandot kan göra mycket mer än att ladda ner filer. Ta reda på vad som curl
kan, och när du ska använda det istället för wget
.
curl vs wget: Vad är skillnaden?
Människor kämpar ofta för att identifiera de relativa styrkorna för wget
och curl
kommandona. Kommandona har viss funktionell överlappning. De kan var och en hämta filer från avlägsna platser, men det är där likheten slutar.
wget
är ett fantastiskt verktyg för nedladdning av innehåll och filer. Det kan ladda ner filer, webbsidor och kataloger. Den innehåller intelligenta rutiner för att korsa länkar på webbsidor och rekursivt ladda ner innehåll över en hel webbplats. Det är oöverträffat som en nedladdningshanterare för kommandoraden.
curl
tillgodoser ett helt annat behov. Ja, det kan hämta filer, men det kan inte rekursivt navigera på en webbplats som letar efter innehåll att hämta. Vad som curl
faktiskt gör är att låta dig interagera med fjärrsystem genom att göra förfrågningar till dessa system och hämta och visa deras svar för dig. Dessa svar kan mycket väl vara webbsidans innehåll och filer, men de kan också innehålla data som tillhandahålls via en webbtjänst eller API som ett resultat av "frågan" som ställs av curlbegäran.
Och curl
är inte begränsat till webbplatser. curl
stöder över 20 protokoll, inklusive HTTP, HTTPS, SCP, SFTP och FTP. Och utan tvekan, på grund av sin överlägsna hantering av Linux-rör, curl
kan det lättare integreras med andra kommandon och skript.
Författaren till curl
har en webbsida som beskriver skillnaderna han ser mellan curl
och wget
.
Installera lock
Av de datorer som används för att undersöka den här artikeln hade Fedora 31 och Manjaro 18.1.0 curl
redan installerats. curl
måste installeras på Ubuntu 18.04 LTS. På Ubuntu kör du det här kommandot för att installera det:
sudo apt-get install curl
Curl-versionen
De --version
alternativet gör curl
redovisa sin version. Den listar också alla protokoll som den stöder.
curl --version
Hämtar en webbsida
Om vi pekar curl
på en webbsida kommer den att hämta den åt oss.
curl //www.bbc.com
Men dess standardåtgärd är att dumpa den till terminalfönstret som källkod.
Akta dig : Om du inte säger att curl
du vill ha något lagrat som en fil kommer det alltid att dumpa det till terminalfönstret. Om filen den hämtar är en binär fil kan resultatet bli oförutsägbart. Skalet kan försöka tolka några av bytevärdena i den binära filen som styrtecken eller flykt-sekvenser.
Spara data i en fil
Låt oss säga till curl att omdirigera utdata till en fil:
curl //www.bbc.com> bbc.html
Den här gången ser vi inte den hämtade informationen, den skickas direkt till filen för oss. Eftersom det inte finns någon terminalfönsterutgång att visa, curl
matar ut en uppsättning förloppsinformation.
Det gjorde det inte i det föregående exemplet eftersom framstegsinformationen skulle ha spridits över webbsidans källkod, så curl
undertryckte den automatiskt.
I det här exemplet curl
upptäcker det att utdata omdirigeras till en fil och att det är säkert att generera framstegsinformation.
Informationen är:
- % Totalt : Det totala beloppet som ska hämtas.
- % Mottagen : Procentandelen och faktiska värden för de data som hittills hämtats.
- % Xferd : Procentandelen och faktiskt skickad om data laddas upp.
- Genomsnittlig hastighet Dload : Den genomsnittliga nedladdningshastigheten.
- Medelhastighetsuppladdning : Den genomsnittliga uppladdningshastigheten.
- Tid totalt : Den uppskattade totala varaktigheten för överföringen.
- Tidsförbrukning : Förfluten tid hittills för denna överföring.
- Tid kvar : Beräknad tid kvar för överföringen att slutföras
- Aktuell hastighet : Den aktuella överföringshastigheten för denna överföring.
Eftersom vi omdirigerade utdata från curl
till en fil har vi nu en fil som heter "bbc.html."
Genom att dubbelklicka på den filen öppnas din standardwebbläsare så att den hämtade webbsidan visas.
Observera att adressen i webbläsarens adressfält är en lokal fil på den här datorn, inte en fjärrwebbplats.
Vi behöver inte omdirigera utdata för att skapa en fil. Vi kan skapa en fil med hjälp av -o
alternativet (output) och berätta curl
att skapa filen. Här använder vi -o
alternativet och tillhandahåller namnet på den fil som vi vill skapa “bbc.html.”
curl -o bbc.html //www.bbc.com
Using a Progress Bar To Monitor Downloads
To have the text-based download information replaced by a simple progress bar, use the -#
(progress bar) option.
curl -x -o bbc.html //www.bbc.com
Restarting an Interrupted Download
It is easy to restart a download that has been terminated or interrupted. Let’s start a download of a sizeable file. We’ll use the latest Long Term Support build of Ubuntu 18.04. We’re using the --output
option to specify the name of the file we wish to save it into: “ubuntu180403.iso.”
curl --output ubuntu18043.iso //releases.ubuntu.com/18.04.3/ubuntu-18.04.3-desktop-amd64.iso
The download starts and works its way towards completion.
If we forcibly interrupt the download with Ctrl+C
, we’re returned to the command prompt, and the download is abandoned.
To restart the download, use the -C
(continue at) option. This causes curl
to restart the download at a specified point or offset within the target file. If you use a hyphen -
as the offset, curl
will look at the already downloaded portion of the file and determine the correct offset to use for itself.
curl -C - --output ubuntu18043.iso //releases.ubuntu.com/18.04.3/ubuntu-18.04.3-desktop-amd64.iso
The download is restarted. curl
reports the offset at which it is restarting.
Retrieving HTTP headers
With the -I
(head) option, you can retrieve the HTTP headers only. This is the same as sending the HTTP HEAD command to a web server.
curl -I www.twitter.com
This command retrieves information only; it does not download any web pages or files.
Downloading Multiple URLs
Using xargs
we can download multiple URLs at once. Perhaps we want to download a series of web pages that make up a single article or tutorial.
Copy these URLs to an editor and save it to a file called “urls-to-download.txt.” We can use xargs
to treat the content of each line of the text file as a parameter which it will feed to curl
, in turn.
//tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-ubuntu#0 //tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-ubuntu#1 //tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-ubuntu#2 //tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-ubuntu#3 //tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-ubuntu#4 //tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-ubuntu#5
This is the command we need to use to have xargs
pass these URLs to curl
one at a time:
xargs -n 1 curl -O < urls-to-download.txt
Note that this command uses the -O
(remote file) output command, which uses an uppercase “O.” This option causes curl
to save the retrieved file with the same name that the file has on the remote server.
The -n 1
option tells xargs
to treat each line of the text file as a single parameter.
When you run the command, you’ll see multiple downloads start and finish, one after the other.
Checking in the file browser shows the multiple files have been downloaded. Each one bears the name it had on the remote server.
RELATED:How to Use the xargs Command on Linux
Downloading Files From an FTP Server
Using curl
with a File Transfer Protocol (FTP) server is easy, even if you have to authenticate with a username and password. To pass a username and password with curl
use the -u
(user) option, and type the username, a colon “:”, and the password. Don’t put a space before or after the colon.
This is a free-for-testing FTP server hosted by Rebex. The test FTP site has a pre-set username of “demo”, and the password is “password.” Don’t use this type of weak username and password on a production or “real” FTP server.
curl -u demo:password ftp://test.rebex.net
curl
figures out that we’re pointing it at an FTP server, and returns a list of the files that are present on the server.
The only file on this server is a “readme.txt” file, of 403 bytes in length. Let’s retrieve it. Use the same command as a moment ago, with the filename appended to it:
curl -u demo:password ftp://test.rebex.net/readme.txt
The file is retrieved and curl
displays its contents in the terminal window.
In almost all cases, it is going to be more convenient to have the retrieved file saved to disk for us, rather than displayed in the terminal window. Once more we can use the -O
(remote file) output command to have the file saved to disk, with the same filename that it has on the remote server.
curl -O -u demo:password ftp://test.rebex.net/readme.txt
The file is retrieved and saved to disk. We can use ls
to check the file details. It has the same name as the file on the FTP server, and it is the same length, 403 bytes.
ls -hl readme.txt
RELATED:How to Use the FTP Command on Linux
Sending Parameters to Remote Servers
Some remote servers will accept parameters in requests that are sent to them. The parameters might be used to format the returned data, for example, or they may be used to select the exact data that the user wishes to retrieve. It is often possible to interact with web application programming interfaces (APIs) using curl
.
As a simple example, the ipify website has an API can be queried to ascertain your external IP address.
curl //api.ipify.org
By adding the format
parameter to the command, with the value of “json” we can again request our external IP address, but this time the returned data will be encoded in the JSON format.
curl //api.ipify.org?format=json
Here’s another example that makes use of a Google API. It returns a JSON object describing a book. The parameter you must provide is the International Standard Book Number (ISBN) number of a book. You can find these on the back cover of most books, usually below a barcode. The parameter we’ll use here is “0131103628.”
curl //www.googleapis.com/books/v1/volumes?q=isbn:0131103628
The returned data is comprehensive:
Sometimes curl, Sometimes wget
If I wanted to download content from a website and have the tree-structure of the website searched recursively for that content, I’d use wget
.
Om jag ville interagera med en fjärrserver eller API och eventuellt ladda ner några filer eller webbsidor, skulle jag använda curl
. Speciellt om protokollet var ett av många som inte stöds av wget
.