Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Download and Extract Tar Files with One Command in Linux
We can download any required file from the web using the Linux terminal. But many times it is found that the downloaded file is a zipped file which is in tar format. In this article we will see how to download and extract the file in a single command.
Using wget and tar
The wget command downloads the data from the given URL while the tar command does the extraction of the tar.gz files. By combining them with a pipe, we can download and extract in one operation ?
$ wget -c https://www.metoffice.gov.uk/hadobs/hadisd/v300_2018f/data/WMO_200000-249999.tar.gz -O - | sudo tar -xz
Running the above code gives us the following result ?
--2020-01-01 07:25:18-- https://www.metoffice.gov.uk/hadobs/hadisd/v300_2018f/data/WMO_200000-249999.tar.gz Resolving www.metoffice.gov.uk (www.metoffice.gov.uk)... 104.80.55.230 Connecting to www.metoffice.gov.uk (www.metoffice.gov.uk)|104.80.55.230|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 454079101 (433M) [application/x-gzip] Saving to: 'STDOUT' - 100%[================================================================================>] 433.04M 4.23MB/s in 1m 46s 2020-01-01 07:27:04 (4.10 MB/s) - written to stdout [454079101/454079101]
Next we list the extracted files to verify the result ?
$ ls -lrt
Running the above code gives us the following result ?
total 500040 -rw-r--r-- 1 10013 users 948807 Jan 26 2019 hadisd.3.0.0.2018f_19310101-20190101_200660-99999.nc.gz -rw-r--r-- 1 10013 users 1296563 Jan 26 2019 hadisd.3.0.0.2018f_19310101-20190101_200490-99999.nc.gz -rw-r--r-- 1 10013 users 2298004 Jan 26 2019 hadisd.3.0.0.2018f_19310101-20190101_200460-99999.nc.gz ...... ......
Command Options Explained
Let's break down the command options used ?
| Option | Command | Purpose |
|---|---|---|
-c |
wget | Continue partial downloads |
-O - |
wget | Output to stdout (pipe to next command) |
-x |
tar | Extract files from archive |
-z |
tar | Handle gzip compression |
Using curl
We can also use curl in place of wget in the above example. Again it is a single command ?
$ sudo curl https://www.metoffice.gov.uk/hadobs/hadisd/v300_2018f/data/WMO_200000-249999.tar.gz | sudo tar -xz
Running the above code gives us the following result ?
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 433M 100 433M 0 0 5217k 0 0:01:24 0:01:24 --:--:-- 5370k
Comparison
| Tool | Advantages | Best For |
|---|---|---|
wget |
Resume downloads, better progress display | Large files, unreliable connections |
curl |
More protocols, simpler syntax | Quick downloads, scripting |
Conclusion
Both wget and curl can download and extract tar files in a single command using pipes. Use wget for large files with resume capability, or curl for simpler, quick operations.
