how to use wget

How to Use wget

Introduction

In this tutorial, we will learn how to use wget – a command-line utility for downloading files from the web. wget supports recursive downloading, meaning it can download not only a single file but also all the files and directories within it. It is available on most Unix-like systems, including Linux and macOS.

Installation

To install wget on your system, follow these steps:

  1. Linux: Open a terminal and run the following command:
    sudo apt-get install wget

  2. macOS: Open a terminal and install Homebrew if you haven’t already:
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    Then, run the following command to install wget:
    brew install wget

Basic Usage

To download a file using wget, use the following syntax:
wget [options] [URL]

Here are some commonly used options:

  • -P [directory]: Specifies the directory to save the downloaded file.
  • -r: Enables recursive downloading, allowing wget to follow links and download all files in a directory.
  • -np: Disables downloading of parent directories when using recursive mode. Useful to prevent downloading files from external websites.
  • -c: Enables resume support, allowing wget to continue a previous interrupted download.
  • -O [filename]: Specifies the name to save the downloaded file as.

Examples

  1. Download a single file:
    wget https://example.com/file.txt

  2. Download a file and save it with a different name:
    wget -O new_name.txt https://example.com/file.txt

  3. Download an entire website:
    wget -r https://example.com

  4. Download files from a website, but only from a specific directory:
    wget -r -np https://example.com/files/

  5. Resume a previously interrupted download:
    wget -c https://example.com/large_file.zip

Conclusion

You have learned the basics of using wget to download files from the web. wget is a powerful tool with many more options and features. To explore its full capabilities, refer to the official documentation. Happy downloading!