Wget(Website get) is a Linux command line tool to download any file which is available through a network which has a hostname or IP address. With wget command we can download from an FTP or HTTP site as this supports many protocols like FTP, HTTP, https, ftps etc. By default wget command downloads files to the present working directory where you execute the command. This is a bit annoying as we have to move the downloaded file to a specific folder or we have to go the directory where you want that file downloaded and use wget command. In this post, we will see how to download to a specific location in the system.
wget command syntax:
wget <URL>
To get downloaded file to a specific directory we should use -P or –directory-prefix=prefix. From wget man pages.
-P prefix --directory-prefix=prefix Set directory prefix to prefix. The directory prefix is the directory where all other files and subdirectories will be saved to, i.e. the top of the retrieval tree. The default is . (the current directory).
The syntax for downloading to a specific directory.
wget -P <path> <URL>
Example:
wget -P /opt/mykernel/ https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.16.13.tar.xz
Output:
wget -P /opt/mykernel/ https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.16.13.tar.xz --2018-06-02 17:56:52-- https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.16.13.tar.xz Resolving cdn.kernel.org... 151.101.1.176, 151.101.65.176, 151.101.129.176, ... Connecting to cdn.kernel.org|151.101.1.176|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 103054608 (98M) [application/x-xz] Saving to: ‘/opt/mykernel/linux-4.16.13.tar.xz’ linux-4.16.13.tar.xz 11%[=======> ] 10.91M 1.19MB/s eta 75s
How about if you want to download a file to a specific directory with a specific name. Downloading as a specific file is required when the downloaded file does not have a specific name. Sometimes the files which we download will not have a specific name. For example, when we try to download firefox browser we will get the file name as “index.html?product=firefox-latest-ssl ”
wget https://download.mozilla.org/?product=firefox-latest-ssl&os=osx&lang=en-US [1] 49071 [2] 49072 Redirecting output to ‘wget-log’. [2]+ Done os=osx [16268] sanne@Surendras-MacBook-Pro:/tmp > fg wget https://download.mozilla.org/?product=firefox-latest-ssl --2018-06-02 18:05:16-- https://download.mozilla.org/?product=firefox-latest-ssl Resolving download.mozilla.org... 35.165.249.56, 52.11.248.173, 35.166.15.94 Connecting to download.mozilla.org|35.165.249.56|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://download-installer.cdn.mozilla.net/pub/firefox/releases/60.0.1/win32/en-US/Firefox%20Setup%2060.0.1.exe [following] --2018-06-02 18:05:17-- https://download-installer.cdn.mozilla.net/pub/firefox/releases/60.0.1/win32/en-US/Firefox%20Setup%2060.0.1.exe Resolving download-installer.cdn.mozilla.net... 54.230.247.220 Connecting to download-installer.cdn.mozilla.net|54.230.247.220|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 36120040 (34M) [application/octet-stream] Saving to: ‘index.html?product=firefox-latest-ssl’ index.html?product=firefox-latest-ssl 100%[============================================================================>] 34.45M 1.19MB/s in 29s 2018-06-02 18:05:46 (1.19 MB/s) - ‘index.html?product=firefox-latest-ssl’ saved [36120040/36120040]
This is a bit annoying as we have to figure out what is the file name. With wget’s -O or –output-document option we can specify the filename we are going to save the file.
wget -O filename <URL>
Example:
wget -O firefox.tar.gz https://download.mozilla.org/?product=firefox-latest-ssl&os=osx&lang=en-US [1] 49268 [2] 49269 Redirecting output to ‘wget-log.1’. [2]+ Done os=osx [16273] sanne@Surendras-MacBook-Pro:/tmp > fg wget -O firefox.tar.gz https://download.mozilla.org/?product=firefox-latest-ssl --2018-06-02 18:09:02-- https://download.mozilla.org/?product=firefox-latest-ssl Resolving download.mozilla.org... 35.166.15.94, 35.165.249.56, 52.11.248.173 Connecting to download.mozilla.org|35.166.15.94|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://download-installer.cdn.mozilla.net/pub/firefox/releases/60.0.1/win32/en-US/Firefox%20Setup%2060.0.1.exe [following] --2018-06-02 18:09:03-- https://download-installer.cdn.mozilla.net/pub/firefox/releases/60.0.1/win32/en-US/Firefox%20Setup%2060.0.1.exe Resolving download-installer.cdn.mozilla.net... 54.230.247.220 Connecting to download-installer.cdn.mozilla.net|54.230.247.220|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 36120040 (34M) [application/octet-stream] Saving to: ‘firefox.tar.gz’ firefox.tar.gz 35%[==========================> ] 12.21M 1.20MB/s eta 20s We can club -P and -O to download to specific directory with a specific file name.
In our next post, we will see some other exiting command example.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018