Wget is commonly used to download files & directories from remote locations. However, sometimes you may need to download directory as well as subdirectories using wget. In this article, we will look at how to download directory & subdirectories using wget.
What is Wget?
Wget command is a useful Linux tool that allows you to easily get content from web servers. It supports data transfer over HTTP, HTTPS as well as FTP. You can use them to download files as well as directories. It allows you to download files even if you are not logged into Linux. Therefore, you can easily use it in shell scripts and cronjobs, as per your requirement.
How to Download Directory & Subdirectories using Wget
Here are the steps to download directory & subdirectories using wget.
Typically, if you want to download directory & all subdirectories using wget command, you need to use -r option for recursive file transfer.
$ sudo wget -r /path/to/dir
Here is an example.
$ wget -r --no-parent http://example.com/projects/
You may also use –no-parent option to prevent wget from downloading parent directories.
However, if you only want to download only the directory and not subdirectories, use -l1 option
$ wget -r -l1 --no-parent http://example.com/projects/
If you want to also download subdirectories from level 1, then use -l2 option also.
$ wget -r -l1 -l2 --no-parent http://example.com/projects/
Similarly, use l3 option to download level 2 subfolders, etc.
However, please be careful not to use l0 (el zero) option otherwise wget command will download all parent folders & files, even from other websites.
That’s it. In this short article, we have seen how to download directory & subdirectories using wget command.