How to Download and Extract Tar Files with One Command in Linux


We can download any required file form 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 form the given URL while the tar command does the extraction of the tar.gz files.

$ 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
…….
……..

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

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
…….
……..

Updated on: 03-Jan-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements