How to Download Specific File Extensions Using Wget on Ubuntu
This guide explains how to use the wget command in Ubuntu to download only files with specific extensions. You will learn the necessary flags to filter content during recursive downloads, saving time and bandwidth. By the end, you will know exactly how to target formats like PDF, MP3, or ZIP without retrieving unnecessary data.
To download files of a specific extension, you must combine the
recursive flag with the accept flag. The primary command structure uses
-r for recursive downloading and -A to specify
the allowed file extensions.
Here is the basic syntax:
wget -r -A .extension https://example.comFor example, to download only PDF files from a website, run the following command:
wget -r -A .pdf https://example.com/documentsYou can also specify multiple extensions by separating them with commas. To download both JPEG and PNG images, use:
wget -r -A .jpg,.jpeg,.png https://example.com/imagesIt is important to note that the -r flag is usually
required for the -A flag to function effectively as a
filter. Without recursion, wget typically downloads the single file
specified regardless of the accept list. Additionally, you may want to
use -np (no-parent) to prevent ascending to the parent
directory during recursion.
A complete robust command looks like this:
wget -r -np -A .zip https://example.com/files/This ensures you only get ZIP files from the specified directory without climbing up the directory tree. Using these flags together allows for precise control over what data is retrieved to your Ubuntu system.