Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 49

Downloading Files with curl and wget

Recommended resources

Hello! Welcome to the next lesson in our journey through practical network analysis.

In our last session, we learned how to use ss and netstat to identify which services are listening on a target system's network ports. This is like finding the doors and windows on a building. Today, we'll learn how to open those doors and grab what's inside, especially when we find an open web port (like 80 or 443).

This lesson directly addresses the learning outcome: Download files from the web using curl and wget. These two commands are fundamental tools in any ethical hacker's or system administrator's toolkit. We'll explore what each tool does best, their key features, and when you should choose one over the other.

1. Introduction to wget and curl

Before diving into the commands, let's establish what these tools are and check that you have them. wget and curl are both command-line utilities for transferring data using various network protocols, most commonly HTTP and HTTPS.

  • wget (Web Get) is a straightforward tool whose primary purpose is to download files from the internet. It's simple, robust, and excellent for downloading entire websites or large files.
  • curl (Client for URL) is a more versatile tool. While it can download files, it's often used for more general data transfer, such as interacting with APIs or viewing the raw content of a webpage directly in your terminal. Think of it as a Swiss Army knife for data transfer.

As an Arch Linux user, you likely have these installed. You can verify with which wget and which curl. If not, you can easily install them:

sudo pacman -S wget curl

2. wget: The Reliable Downloader

Let's start with wget. Its design philosophy is simple: get the file and save it. This makes it very intuitive for its main purpose.

Linux Crash Course - The wget Command

The video 'Linux Crash Course - The wget Command' from Learn Linux TV provides an excellent, practical introduction to wget and its most important features.

Please watch the following segments to get a strong grasp of wget: 00:52 - 01:15: Introduction to wget and its usefulness. 04:58 - 05:47: Basic file download. 05:55 - 07:17: Downloading and saving with a custom filename using -O. 07:28 - 08:31: Saving to a specific directory with -P. 08:38 - 11:14: Resuming an interrupted download with -c – a very powerful feature for large files. 11:19 - 13:11: Downloading multiple files from a list with -i.

Key wget Commands

Based on the video, here are the essential wget commands you should know:

Action Command Example Explanation
Basic Download wget <URL> Downloads the file to your current directory with its original name.
Rename File wget -O new_name.zip <URL> Downloads the file and saves it as new_name.zip. Note the uppercase -O.
Specify Directory wget -P /tmp/downloads <URL> Downloads the file into the /tmp/downloads directory. Note the uppercase -P.
Resume Download wget -c <URL> Continues a partially downloaded file. Essential for unstable connections or large files.
Download from List wget -i urls.txt Reads a list of URLs from urls.txt and downloads each one. Incredibly useful for automation.
Downloading Multiple Files with `wget -i`
This terminal output shows `wget -i` in action, downloading multiple files listed in a text file. This is a great example of how `wget` simplifies bulk downloading tasks.

3. curl: The Versatile Data Transfer Tool

Now let's turn to curl. Its default behavior is different and highlights its flexibility: instead of saving a file, it prints the content directly to your terminal's standard output. This is perfect for a quick peek at a file or for piping data to other commands.

Linux Essentials: Curl Fundamentals

The video 'Linux Essentials: Curl Fundamentals' by HackerSploit is geared towards aspiring hackers and provides a great overview of curl's core functionality.

Please watch these key sections to understand how curl works: 03:40 - 06:17: Demonstrates curl's default behavior (printing to screen) and how to save output to a file using -o (lowercase o). 06:17 - 08:39: Shows the two main ways to download files: specifying a filename with -o and using the remote filename with -O (uppercase O). 08:39 - 10:51: Explains how to handle HTTP redirects with -L, a crucial step for many modern websites.

Key curl Commands

Here's a summary of the curl commands from the video:

Action Command Example Explanation
View Content curl <URL> Displays the content of the URL directly in the terminal.
Save to File (Redirection) curl <URL> > file.html Uses standard output redirection to save the content to file.html.
Save with Custom Name curl -o new_name.txt <URL> Downloads the file and saves it as new_name.txt. Note the lowercase -o.
Save with Original Name curl -O <URL> Downloads the file and saves it using the name from the URL. Note the uppercase -O.
Follow Redirects curl -L <URL> If the URL redirects to another location (an HTTP 3xx response), curl will follow it.
Downloading a File with `curl -o`
This image shows the `curl -o` command being used to download a file and rename it. The progress meter gives you useful statistics about the download.

4. wget vs. curl: Choosing the Right Tool

You might be wondering when to use wget and when to use curl. While they have overlapping features, they are designed for different primary use cases.

Wget and Curl for Downloading Files in Linux - A Complete Guide

This article, 'Wget and Curl for Downloading Files in Linux', provides a fantastic summary that compares the two tools. The final section clearly lays out their differences.

Please read the section titled 'Differences Between Curl and wget Usage'. This will help you synthesize what you've learned and understand the design philosophy behind each tool.

Here is a quick summary of the key differences:

  • Primary Use:

    • wget is a downloader. It's built for reliably getting files from the web.
    • curl is a data transfer tool. It's built for interacting with URLs and is often the backbone of scripts that test APIs.
  • Default Behavior:

    • wget <URL> downloads a file.
    • curl <URL> displays the content to standard output.
  • Recursive Downloading:

    • wget is the king of recursive downloads (wget -r). It can mirror entire websites.
    • curl does not support this natively.
  • Underlying Library:

    • curl is powered by libcurl, a library used in thousands of applications (including cars, TVs, and more) to handle data transfers. wget is a standalone program.

Rule of thumb:

  • Use wget when your goal is to download files, especially large ones, multiple files, or entire directories.
  • Use curl when you want to see the content of a URL quickly, test an API endpoint, or use its output in a command pipeline.

5. Hands-On Lab

Time to put this into practice. Open your terminal and let's download some files. I suggest creating a dedicated directory for this practice, for example: mkdir -p ~/practice/downloads && cd ~/practice/downloads.

Activity 1: Download a Hacking Tool with wget
A common task is to download security scripts. Let's grab LinPEAS, a popular privilege escalation checker.

  1. Use wget to download it.
    wget https://github.com/carlospolop/PEASS-ng/releases/latest/download/linpeas.sh
    
  2. Use ls -l to verify the file linpeas.sh has been downloaded.

Activity 2: Resuming a Large Download
Let's simulate downloading a large file that gets interrupted. We'll download the Arch Linux ISO.

  1. Start the download but limit the speed so you have time to cancel it.
    wget --limit-rate=500k http://mirror.cs.pdx.edu/archlinux/iso/latest/archlinux-x86_64.iso
    
  2. After about 10-15 seconds, press Ctrl+C to interrupt the download.
  3. Use ls -l. You'll see a partially downloaded .iso file.
  4. Now, resume the download. wget will see the partial file and pick up where it left off.
    wget -c http://mirror.cs.pdx.edu/archlinux/iso/latest/archlinux-x86_64.iso
    
  5. Let it run for a few more seconds, then you can cancel it again with Ctrl+C. You have now successfully practiced resuming a download.

Activity 3: Inspecting Web Content with curl
Let's see curl's default behavior.

  1. Run the following command. You'll see the raw HTML of the example website printed to your screen.
    curl http://example.com
    
  2. Now, let's handle a redirect. http://google.com will redirect to https://www.google.com.
    
    
    
    # This will just show a "301 Moved" message
    curl http://google.com
    
    
    
    
    # This will follow the redirect and show the actual page content
    curl -L http://google.com
    

Activity 4: Saving Files with curl
Now let's use curl to save the same linpeas.sh script.

  1. Save it with a custom name using -o.
    curl -o linpeas_from_curl.sh -L https://github.com/carlospolop/PEASS-ng/releases/latest/download/linpeas.sh
    
    (We use -L because GitHub release URLs often involve redirects).
  2. Now save it with its original name using -O.
    curl -O -L https://github.com/carlospolop/PEASS-ng/releases/latest/download/linpeas.sh
    
  3. You'll get a warning that linpeas.sh already exists. curl by default won't overwrite. If you wanted to, you'd remove the file first (rm linpeas.sh) before running the command.

Conclusion

You've now mastered two essential command-line tools for interacting with web resources. This skill is critical for everything from downloading security tools and exploit code to scripting interactions with web applications during a penetration test.

Key Takeaways:

  • wget is your go-to tool for downloading files. Its key flags are -O (rename), -P (directory), -c (resume), and -i (input file).
  • curl is a versatile data transfer tool. Its default is to print to the screen. Key flags are -o (save to file), -O (save with original name), and -L (follow redirects).
  • Understanding the fundamental difference in their default behavior (save vs. print) is the key to choosing the right tool for the job.

Next Lesson Preview:
We've found open ports (ss), and now we can download files from them (wget, curl). But what's happening on the wire? How does the data actually look as it travels across the network? In our next lesson, we will dive into network packet analysis by learning how to capture and inspect network packets using tcpdump with basic filters. This will give you an even deeper view into network communications.