Introduction
In this article, we will learn how to download a file from the web onto a Linux server without a desktop environment. We will cover the use of the wget and curl commands to retrieve files using hyperlinks and handle HTTP 301 Moved responses.
Prerequisites
To follow along with this tutorial, you will need access to a Linux server with a command line interface. You do not need a desktop environment, as we will be using command line tools.
Using wget to Download Files
The wget command is a powerful tool for downloading files from the web. It can handle hyperlinks, HTTP and HTTPS protocols, and retry failed downloads. Here is an example of how to use wget to download a file:
wget http://example.com/file.txt
This will download the file located at http://example.com/file.txt to your current working directory on the Linux server.
Handling HTTP 301 Moved Responses
Sometimes, when you try to download a file, you might receive an HTTP 301 Moved response. This means that the file has been moved to a different URL. To handle this, you can use the -t option in wget to specify the maximum number of retries before giving up. Here is an example:
wget -t 5 http://example.com/old-file.txt
This will try to download the file located at http://example.com/old-file.txt up to 5 times before giving up. If the file has been moved, you will need to find the new URL and use that instead.
Using curl to Download Files
The curl command is another popular tool for downloading files from the web. It can also handle hyperlinks, HTTP and HTTPS protocols, and retry failed downloads. Here is an example of how to use curl to download a file:
curl -O http://example.com/file.txt
This will download the file located at http://example.com/file.txt to your current working directory on the Linux server. The -O option tells curl to save the file with the same name as on the web server.
In this article, we have learned how to download files from the web onto a Linux server using the wget and curl commands. We have covered how to handle HTTP 301 Moved responses, and how to use these tools to download files using hyperlinks. These tools are essential for automating file downloads and retrieving files from the web.