DOWNLOAD

How to Download Files with cURL

53

Client URL, or cURL, is a library and command-line utility for transferring data between systems. It supports many protocols and tends to be installed by default on many Unix-like operating systems. Because of its general availability, it is a great choice for when you need to download a file to your local system, especially in a server environment.

In this tutorial, you’ll use the curl command to download a text file from a web server. You’ll view its contents, save it locally, and tell curl to follow redirects if files have moved.

Bạn Đang Xem: How to Download Files with cURL

Read more: Download via curl

Downloading files off of the Internet can be dangerous, so be sure you are downloading from reputable sources. In this tutorial you’ll download files from DigitalOcean, and you won’t be executing any files you download.

Launch an Interactive Terminal!

Step 1 — Fetching remote files

Out of the box, without any command-line arguments, the curl command will fetch a file and display its contents to the standard output.

Let’s give it a try by downloading the cinemaboxhd.org file from cinemaboxhd.org:

  1. curl cinemaboxhd.org/cinemaboxhd.org

You’ll see the file’s contents displayed on the screen:

Xem Thêm : Spotify to MP3 Converter: Download Spotify Songs to MP3 for Free

OutputUser-agent: * Disallow: sitemap: cinemaboxhd.org/cinemaboxhd.org sitemap: cinemaboxhd.org/community/cinemaboxhd.org sitemap: cinemaboxhd.org/community/cinemaboxhd.org sitemap: cinemaboxhd.org/community/cinemaboxhd.org

Give curl a URL and it will fetch the resource and display its contents.

Saving Remote Files

Fetching a file and display its contents is all well and good, but what if you want to actually save the file to your system?

To save the remote file to your local system, with the same filename as the server you’re downloading from, add the -remote-name argument, or use the -O option:

  1. curl -O cinemaboxhd.org/cinemaboxhd.org

Watch more: YouTube Video Cutter

Your file will download:

Xem Thêm : PUBG Mobile for Android

Output % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 286 0 286 0 0 5296 0 -:-:- -:-:- -:-:- 5296

Instead of displaying the contents of the file, curl displays a text-based progress meter and saves the file to the same name as the remote file’s name. You can check on things with the cat command:

  1. cat cinemaboxhd.org

The file contains the same contents you saw previously:

Xem Thêm : Spotify to MP3 Converter: Download Spotify Songs to MP3 for Free

OutputUser-agent: * Disallow: sitemap: cinemaboxhd.org/cinemaboxhd.org sitemap: cinemaboxhd.org/community/cinemaboxhd.org sitemap: cinemaboxhd.org/community/cinemaboxhd.org sitemap: cinemaboxhd.org/community/cinemaboxhd.org

Now let’s look at specifying a filename for the downloaded file.

Step 2 — Saving Remote Files with a Specific File Name

You may already have a local file with the same name as the file on the remote server.

To avoid overwriting your local file of the same name, use the -o or -output argument, followed by the name of the local file you’d like to save the contents to.

Execute the following command to download the remote cinemaboxhd.org file to the locally named cinemaboxhd.org file:

  1. curl -o cinemaboxhd.org cinemaboxhd.org/cinemaboxhd.org

Once again you’ll see the progress bar:

Output % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 286 0 286 0 0 6975 0 -:-:- -:-:- -:-:- 7150

Now use the cat command to display the contents of cinemaboxhd.org to verify it’s the file you downloaded:

  1. cat cinemaboxhd.org

The contents are the same:

Xem Thêm : Spotify to MP3 Converter: Download Spotify Songs to MP3 for Free

OutputUser-agent: * Disallow: sitemap: cinemaboxhd.org/cinemaboxhd.org sitemap: cinemaboxhd.org/community/cinemaboxhd.org sitemap: cinemaboxhd.org/community/cinemaboxhd.org sitemap: cinemaboxhd.org/community/cinemaboxhd.org

Learn More: Bitdefender Antivirus Free for Windows

By default, curl doesn’t follow redirects, so when files move, you might not get what you expect. Let’s look at how to fix that.

Step 3 — Following Redirects

Thus far all of the examples have included fully qualified URLs that include the https:// protocol. If you happened to try to fetch the cinemaboxhd.org file and only specified cinemaboxhd.org, you would not see any output, because DigitalOcean redirects requests from http:// to https://:

You can verify this by using the -I flag, which displays the request headers rather than the contents of the file:

  1. curl -I cinemaboxhd.org/cinemaboxhd.org

The output shows that the URL was redirected. The first line of the output tells you that it was moved, and the Location line tells you where:

OutputHTTP/1.1 301 Moved Permanently Cache-Control: max-age=3600 Cf-Ray: 65dd51678fd93ff7-YYZ Cf-Request-Id: 0a9e3134b500003ff72b9d0000000001 Connection: keep-alive Date: Fri, 11 Jun 2021 19:41:37 GMT Expires: Fri, 11 Jun 2021 20:41:37 GMT Location: cinemaboxhd.org/cinemaboxhd.org Server: cloudflare . . .

You could use curl to make another request manually, or you can use the -location or -L argument which tells curl to redo the request to the new location whenever it encounters a redirect. Give it a try:

  1. curl -L cinemaboxhd.org/cinemaboxhd.org

This time you see the output, as curl followed the redirect:

Xem Thêm : Spotify to MP3 Converter: Download Spotify Songs to MP3 for Free

OutputUser-agent: * Disallow: sitemap: cinemaboxhd.org/cinemaboxhd.org sitemap: cinemaboxhd.org/community/cinemaboxhd.org sitemap: cinemaboxhd.org/community/cinemaboxhd.org sitemap: cinemaboxhd.org/community/cinemaboxhd.org

You can combine the -L argument with some of the aforementioned arguments to download the file to your local system:

  1. curl -L -o cinemaboxhd.org cinemaboxhd.org/cinemaboxhd.org

Warning: Many resources online will ask you to use curl to download scripts and execute them. Before you run any scripts you have downloaded, it’s good practice to check their contents before making them executable and running them. Use the less command to review the code to ensure it’s something you want to run.

Conclusion

curl lets you quickly download files from a remote system. curl supports many different protocols and can also make more complex web requests, including interacting with remote APIs to send and receive data.

You can learn more by viewing the manual page for curl by running man curl.

Watch more: Download sách Writing Strategies for The IELTS test (PDFAdio) Free

Nguồn: https://cinemaboxhd.org
Danh mục: DOWNLOAD

0 ( 0 bình chọn )

IELTS Online Exam & Sample Questions

https://cinemaboxhd.org
IELTS Online Plus, considering your test centre offers this, you will take the Listening, Reading and Writing

Ý kiến bạn đọc (0)

Leave a Reply

Your email address will not be published.

Related Posts

News Post

452. Ledgers Canada

21/07/2022 10:43 75

451. CMB Services

21/07/2022 10:43 73

449. KATM Bookkeeping

21/07/2022 10:41 72

443. Liu & Associates LLP

21/07/2022 10:37 61

442. Liu & Associates

21/07/2022 10:36 56

Xem thêm