Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Download Files to Specific Directory Using Wget?
As frequent users of command-line tools, we often find ourselves needing to download files from the internet. One of the go-to tools for this task is wget, which offers an efficient way to download files from the command line. However, it's important to know how to specify the directory where the downloaded files will be saved.
In this article, we will explore downloading files to a specific directory using wget. We'll examine the different command-line options and parameters that you can use to specify the download directory, along with practical examples to illustrate their usage.
Basic Wget Syntax
The basic syntax of wget is
$ wget [option] [URL]
If wget isn't installed on your Ubuntu or Debian-based system, you can install it by executing the following command
$ sudo apt-get update $ sudo apt-get install wget
Download a File to a Specific Directory
To download files and save them to a specific directory without changing the current working directory, use the -P or --directory-prefix option. This sets the directory prefix where all retrieved files and subdirectories will be saved.
The syntax for the -P command to download files to a specific directory
$ wget -P /path/to/directory URL
Example
$ wget -P /home/user/Downloads https://example.com/file.zip
--2023-02-24 11:25:45-- https://example.com/file.zip Resolving example.com (example.com)... 93.184.216.34 Connecting to example.com (example.com)|93.184.216.34|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 4781945 (4.6M) [application/zip] Saving to: '/home/user/Downloads/file.zip' /home/user/Downloads/file.zip 100%[===============>] 4.56M 2.39MB/s in 1.9s 2023-02-24 11:25:48 (2.39 MB/s) - '/home/user/Downloads/file.zip' saved [4781945/4781945]
Download Multiple Files to a Specific Directory
To download multiple files, simply provide the URLs of the files you want to download as arguments to the command. Use the -P option to specify the directory where you want to save all downloaded files.
$ wget -P /home/user/Downloads https://example.com/file1.zip https://example.com/file2.zip https://example.com/file3.zip
The output will show the download progress for each file sequentially
--2023-03-03 10:22:38-- http://example.com/file1.zip Resolving example.com (example.com)... 93.184.216.34 Connecting to example.com (example.com)|93.184.216.34|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 250 [application/zip] Saving to: '/home/user/Downloads/file1.zip' /home/user/Downloads/file1.zip 100%[===============>] 250 --.-KB/s in 0s 2023-03-03 10:22:39 (13.4 MB/s) - '/home/user/Downloads/file1.zip' saved [250/250] --2023-03-03 10:22:39-- http://example.com/file2.zip Resolving example.com (example.com)... 93.184.216.34 Connecting to example.com (example.com)|93.184.216.34|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 250 [application/zip] Saving to: '/home/user/Downloads/file2.zip' /home/user/Downloads/file2.zip 100%[===============>] 250 --.-KB/s in 0s 2023-03-03 10:22:39 (15.2 MB/s) - '/home/user/Downloads/file2.zip' saved [250/250]
Download Files from Multiple Websites to Different Directories
When downloading files from multiple websites and organizing them into separate directories, you can use multiple -P options with their respective URLs. However, note that each -P option applies to the URL that follows it.
The syntax is
$ wget -P /path/to/directory1 URL1 -P /path/to/directory2 URL2
Resume Interrupted Downloads
When downloading large files, network errors can interrupt the process. The -c or --continue option allows you to resume downloading partially downloaded files, saving time and bandwidth.
$ wget -c -P /home/user/Downloads https://example.com/largefile.zip
--2023-02-24 14:30:00-- https://example.com/largefile.zip Resolving example.com (example.com)... 192.169.82.67 Connecting to example.com (example.com)|192.169.82.67|:443... connected. HTTP request sent, awaiting response... 206 Partial Content Length: 1024000 (1000K), 512000 (500K) remaining [application/zip] Saving to: '/home/user/Downloads/largefile.zip' largefile.zip 50%[======== ] 512.00K 1.20MB/s largefile.zip 100%[===================] 1000K 1.35MB/s in 0.4s 2023-02-24 14:30:01 (1.35 MB/s) - '/home/user/Downloads/largefile.zip' saved [1024000/1024000]
Common Wget Directory Options
| Option | Description | Example |
|---|---|---|
-P /path |
Set directory prefix for downloads | wget -P /home/user/Downloads URL |
-O filename |
Save with specific filename | wget -O /path/myfile.zip URL |
-c |
Continue partial downloads | wget -c -P /downloads URL |
Additional Help
For more information about wget command options, use
$ man wget
Conclusion
Using wget with the -P option provides an efficient way to download files directly to specific directories. Combined with options like -c for resuming downloads, wget becomes a powerful tool for managing file downloads from the command line, helping organize your files without manual intervention.
