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, we will find that it's important to know how to specify the directory where the downloaded files will be saved.

In this article, we will share experiences with downloading files to a specific directory using "Wget." We'll explore the different command-line options and parameters that you can use to specify the download directory, along with practical examples to illustrate their usage. By the end of this article, you'll be equipped with the knowledge to use Wget to download files to a specific directory with ease.

The basic syntax of Wget is 

$ wget [option] [URL]

We assume you have a fundamental understanding of working with command lines and have already installed Wget on your system assuming. If Wget isn't installed on your Ubuntu or Debian-based system, we can install it by executing the following command.

$ sudo apt-get update
$ sudo apt-get install wget

1. Download a File to a Specific Directory

we will explain how to download files and save them to a specific directory without changing the current working directory. To set the directory prefix where all retrieved files and subdirectories will be saved, we can use Wget "-P" or "--directory-prefix" option. This makes it easy to organize your downloads into different folders, without manually moving the files after downloading them.

The syntax for the “-P” command to download files to a specific directory

$ wget -P /path/to/directory URL

By running this command we can get output like this

$ wget -P /home/user/Downloads https://tutorialspoint.com/file.zip
--2023-02-24 11:25:45--  https://tutorialspoint.com/file.zip
Resolvingtutorialspoint.com(tutorialspoint.com)..93.184.216.34,2606:2800:220:1:248:1893:25c8:1946
Connecting to tutorialspoint.com (tutorialspoint.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]

In this unique example, the command is downloading a sample file from the website tutorialspoint.com and saving the file to the specific directory /home/user/Documents. The terminal output shows the progress of the download, including the resolution of the domain name, connection to the server, HTTP request, and the size and type of the downloaded file.

2. Download Multiple Files to a Specific Directory

The Wget command is a powerful command-line tool that can be used to download multiple files from a website or server. To download multiple files, simply we can write the URLs of the files that we want to download as arguments to the command. We can also use the -P option to specify the directory where you want to save the downloaded files.

Input

$ wget -P /home/user/Downloads https://tutorialspoint.com/file1.zip https://tutorialspoint.com/file2.zip https://tutorialspoint.com/file3.zip

In this example, the get command is downloading three files from the website tutorialspoint.com and saving them to the directory /home/user/Downloads. And the output will look like this and contains all the information related to 3 files.

Output

--2023-03-03 10:22:38--  http://tutorialspoint.com/file1.zip
Resolving tutorialspoint.com (tutorialspoint.com)... 93.184.216.34
Connecting to tutorialspoint.com (tutorialspoint.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://tutorialspoint.com/file2.zip
Resolving tutorialspoint.com (tutorialspoint.com)... 93.184.216.34
Connecting to tutorialspoint.com (tutorialspoint.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]
--2023-03-03 10:22:39--  http://tutorialspoint.com/file3.zip
Resolving tutorialspoint.com (tutorialspoint.com)... 93.184.216.34
Connecting to tutorialspoint.com (tutorialspoint.com)|93.184.216.34|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 250 [application/zip]
Saving to: '/home/user/Downloads/file3.zip'
/home/user/Downloads/file3.zip               100%[=================================================================>]     250  --.-KB/s    in 0s      
2023-03-03 10:22:39 (19.4 MB/s) - '/home/user/Downloads/file3.zip' saved [250/250]

3. Download Files from Multiple Websites to Different Directories

When we need to download files from multiple websites and organize them into separate directories, we turn to the Wget command. With Wget, we can easily download files from different websites and save them into different directories without the need to manually move each file after download.

To do this, we provide a list of URLs as arguments to the command, followed by the directory path for each URL using the -P option. For instance, let's say we want to download three files: file1 from tutorialspoint1.com, file2 from tutorialspoint2.com, and file3 from tutorialspoint3.com. we want to save file1 to /home/user/tutorialspoint1, file2 to /home/user/tutorialspoint2, and file3 to /home/user/tutorialspoint3.

To achieve this, we will use the following command −

$ wget -P /path/to/directory1 URL1 -P /path/to/directory2 URL2 -P /path/to/directory3 URL3 ...

Output for the following command 

--2023-02-25 06:00:00--  URL1
Resolving URL1 (URL1)... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12345 (12K) [text/plain]
Saving to: '/path/to/directory1/file1.txt'
/path/to/directory1/file1.txt     100%[======================================>]  12.05K  --.-KB/s    in 0s --2023-02-25 06:00:01--  URL2
Resolving URL2 (URL2)... connected.
HTTP request sent, awaiting response... 200 OK
Length: 67890 (66K) [text/plain]
Saving to: '/path/to/directory2/file2.txt'
/path/to/directory2/file2.txt     100%[======================================>]  66.28K --.-KB/s    in 0s --2023-02-25 06:00:02--  URL3
Resolving URL3 (URL3)... connected.
HTTP request sent, awaiting response... 200 OK
Length: 13579 (13K) [text/plain]
Saving to: '/path/to/directory3/file3.txt'
/path/to/directory3/file3.txt     100%[======================================>]  13.26K  --.-KB/s    in 0s      

Using wget to download large files can be a time-consuming task, and network errors can make it even more frustrating. The “-c or --continue” command in wget allows you to resume downloading partially downloaded files. This feature is useful when you need to pause or restart a download, saving you time and effort.

Input Command 

$ wget -c https://tutorialspoint.com/myfile.zip

Output Command 

--2023-02-24 14:30:00--  https://tutorialspoint.com/myfile.zip
Resolving tutorialspoint.com (tutorialspoint.com)... 192.169.82.67
Connecting to tutorialspoint.com (tutorialspoint.com)|192.169.82.67|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1024000 (1000K) [application/zip]
Saving to: 'myfile.zip'
myfile.zip           0%[                    ]       0  --.-KB/s               
myfile.zip          25%[==                  ] 254.49K  1.09MB/s               
myfile.zip          50%[========            ] 512.00K  1.20MB/s               
myfile.zip          75%[==============>     ] 768.00K  1.27MB/s               
myfile.zip         100%[===================>] 1000K   1.35MB/s    in 0.8s    
2023-02-24 14:30:01 (1.35 MB/s) - 'myfile.zip' saved [1024000/1024000]

For more information, about wget command we can write man wget command.

$ man wget 

Conclusion

In conclusion, using the wget command can be a convenient and efficient way to download files from the internet to a specific directory on your system. By using the -P command, you can specify the directory where you want the downloaded files to be saved. Overall, mastering the use of wget and its various options can significantly simplify and streamline your file-downloading process. Whether you are a seasoned Linux user or just getting started, understanding how to use wget can be a valuable skill to have in your toolkit.

Updated on: 27-Jul-2023

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements