Linux operating systems have been developed to offer us a safe and fully functional working environment by allowing us to extend their capabilities thanks to free code. In addition to this, there are many tools and commands that facilitate the management and control of various tasks that we perform daily..
One of these tasks, although it may not be frequent, is the download and extraction of files from the internet where the tar command is raised as one of the most ideal options for the optimal execution of this process, offering us unique and practical features.
In this tutorial we will see how to use the tar command and its different options to optimize the download of files from the network..
What is tar
The tar (Tape Archiver) command is an official POSIX format through which it will be possible to execute tasks such as:
- Use with other commands for file compression
- Download multimedia items
- Transfer files and much more.
When using tar we have different levels of compression such as
Without compression
It refers to the uncompressed files which end in the .tar extension.
Gzip compression
This format is the most used for implementation with tar due to its speed and efficiency when extracting files.
These files will have the extension tar.gz or .tgz.
Bzip2 compression
Although Bzip2 is slower than Gzip it offers a better level of compression and its extension is .tar.bz2.
Lzip Compression (LZMA)
It is a hybrid that allows us to have the Gzip speed together with the compression levels of Bzip2 but its use is not frequent.
Some cases where the use of tar is effective are
Now, when we download a tar file from the network, we must have ideal tools that allow us to manage and administer it in a simple way, and what better way to use wget or curl..
1. How to download and extract files using the Wget Linux command
Wget or GNU Wget, is basically a free software package that allows us to manage files using HTTP, HTTPS, FTP and FTPS protocols.
With Wget we have a number of advantages such as
- Manage files in different languages ​​thanks to the fact that it is based on NLS
- Supports HTTP proxies and cookies
- It allows to be executed in unattended mode.
In this first example, we will see how to download and unzip a rar file.
To do this we will execute the following line:
wget -c http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz -O - | tar -xz
In this case we will download the Geolite database utility.
If we wish we can list the contents of the downloaded file:
We will detail what has been executed in this line:
-OR
Parameter O indicates the file where the data is to be written and when placing the sign - we indicate that it is stored in a standard output.
-x
This parameter allows us to activate file extraction
-z
Its function is to compress or decompress the selected file.
If we want to extract the contents of the downloaded file in a different path, it will be necessary to add the -C parameter as follows:
sudo wget -c http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz -O - | sudo tar -xz -C (new route)
Wget is a tool that allows us to execute various actions, for example, if we want to download the file before its extraction, we will execute the following:
sudo wget -c http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz && tar -xzf GeoLite2-Country.tar.gz
We can see that in this case the file has been downloaded in tar.gz format but not decompressed.
2. How to download and extract files using Curl Linux
This command has been developed as a command line utility or script to transfer data securely.
Curl supports protocols such as HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP and many more.
To download a file with the curl command and proceed to its immediate decompression we will execute the following:
sudo curl http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz | tar -xz
We have used ls to list that the file has been decompressed.
As with wget it is possible to specify a new path where the file has to be extracted, for this we can use some of the following commands:
sudo curl http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz | sudo tar -xz -C (Directory) sudo curl http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz && sudo tar -xzf GeoLite2-Country.tar.gz -C (Directory)
Thus we have understood how to use tar along with other commands for downloading and decompressing files in Linux.