Wget + Make

Introduction

You can read about make separately in the article make

and about wget in the article wget

Study those articles first if your knowledge of the subject is not very extensive.

In this article you can study several examples of using them together

Checking that a directory exists

It is useful to check that the directory you want to download something into exists. If it does not, you can create it

LINUX_DIR=/home/$$(whoami)/Downloads/linux_isos/ .PHONY: centos7-readme centos7-readme: # Download centos7_README.txt $(check_dir) @if [ ! -f $(LINUX_DIR)centos7_README.txt ]; then \ wget -c http://centos.mirror.far.fi/7.9.2009/isos/x86_64/0_README.txt \ -O $(LINUX_DIR)centos7_README.txt; \ else \ echo "centos7_README.txt already exists"; \ fi define check_dir if [ ! -d $(LINUX_DIR) ]; then \ echo "INFO: Creating dir "$(LINUX_DIR); \ mkdir -p $(LINUX_DIR); \ fi endef

Wildcard: downloading by a partial name

Part of the name of a file in a repository may change from time to time.

For example, a new version comes out with different digits in its name.

I often need to download the latest version of a piece of software, whatever digits its name has. I rename the downloaded file to some easy-to-remember name.

The use of * , a wildcard, helps with this

Suppose we need the file
ubuntu-18.00.1-desktop-amd64.list
but a new version may already have come out:
ubuntu-18.04.6-desktop-amd64.list

in short, we do not want to put the exact number into the script.

We want to download the current version of the file and rename it to ubuntu-desktop.list if the file already exists there is no need to download it, a warning has to be shown.

For the example let us use the repository Ubuntu from the company Yandex https://mirror.yandex.ru/ubuntu-releases/bionic/

Note that not every repository supports wildcards; I tried the repository from Timeweb - https://mirror.timeweb.ru/ubuntu-releases/bionic/ and the script did not work.

Let us assume that this script works with the support of the service Yandex Rent follow the link if you rent/look for housing.

LINUX_DIR=/home/$$(whoami)/Downloads/linux_isos/ UBUNTU_REPO="mirror.yandex.ru" … .PHONY:download-ubuntu-desktop-list download-ubuntu-desktop-list: # Download ubuntu-desktop.list $(check_dir) @if [ ! -f $(LINUX_DIR)ubuntu-desktop.list ]; then \ wget -r -np "https://$(UBUNTU_REPO)/ubuntu-releases/bionic/ubuntu-18.04.6-desktop-amd64.list" \ -A "*-desktop-amd64.list"; \ else \ echo "WARNING: ubuntu-desktop.list already exists"; \ fi sudo mv ./$(UBUNTU_REPO)/ubuntu-releases/bionic/* $(LINUX_DIR)ubuntu-desktop.list @rm -rf $(UBUNTU_REPO) define check_dir @if [ ! -d $(LINUX_DIR ]; then \ echo "INFO: Creating dir "$(LINUX_DIR; \ mkdir -p $(LINUX_DIR); \ else echo "INFO: Directory "$(LINUX_DIR" already exists"; \ fi endef

You can check the result of the download with the command

ls /home/$(whoami)/Downloads/linux_isos

centos7_README.txt ubuntu-desktop.list

Explanations: first the whole hierarchy down to the file was downloaded from the repository ubuntu-18.04.6-desktop-amd64.list

If at that moment we ran

tree mirror.timeweb.ru

we would see

mirror.timeweb.ru └── ubuntu-releases └── bionic └── ubuntu-18.04.6-desktop-amd64.list

Then the mv command moves the file to the directory we need and renames it.

The last step is to delete the hierarchy that is no longer needed

Article author: Andrei Olegovich