How to extract multiple tar ball (*.tar.gz) files in directory on Linux or Unix

I have tried tar -xvf *.tar.gz command, but getting an error that read as:
tar (child): *.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now

How can I extract multiple *.tar.gz files in directory using Linux or Unix-shell prompt?

ADVERTISEMENTS

Linux or Unix-like system use the tar command to list, test, or extract files from a tar ball archive, commonly found on Unix-like systems.

The problem with multiple tarballs files on Linux/Unix

Assuming that you have four file in the current directory as follows:

  1. conf.tar.gz
  2. mysql.tar.gz
  3. www.tar.gz

Let us verify it with the ls command $ ls
Sample outputs:

conf.tar.gz  mysql.tar.gz  www.tar.gz

To untar all *.tar.gz file, enter:
$ tar xvf *.gz
OR
$ tar -zxvf *.tar.gz
OR
$ tar xvf "*.gz"
OR
$ tar -zxvf '*.gz'
Sample outputs:

Fig.01: Extract multiple .tar.gz files

Fig.01: Extract multiple .tar.gz files

Method #1: Untar multiple tarballs using bash for loop

Use the bash for loop as follows:
for f in *.tar.gz; do tar xf "$f"; done
OR
for f in *.tar.gz; do tar -xvf "$f"; done
OR
for f in *.tgz; do tar -xzvf "$f"; done
OR
for f in *.tar.bz2; do tar -xvf "$f"; done
OR
for f in *.tar.bz2; do tar -xjvf "$f"; done
OR
for f in *.tar.xz; do tar -xvf "$f"; done
OR
for f in *.tar.xz; do tar -xvJf "$f"; done
Verify it:
ls
Sample outputs:

conf.tar.gz  etc  home  mysql.tar.gz  root  www.tar.gz

Method #2: Untar multiple tarballs using bash/sh/ksh pipes

The syntax is:
cat *.tar.gz | tar zxvf - -i
cat *.tgz | tar zxvf - -i
cat *.tar.xz | tar Jxvf - -i
cat *.tar.bz2 | tar jxvf - -i

Sample outputs:

etc/mysql/
etc/mysql/ssl/
etc/mysql/ssl/server-req.pem
etc/mysql/ssl/ca-key.pem
etc/mysql/ssl/client-key.pem
etc/mysql/ssl/server-key.pem
etc/mysql/ssl/server-cert.pem
etc/mysql/ssl/client-cert.pem
etc/mysql/ssl/client-req.pem
etc/mysql/ssl/ca-cert.pem
etc/mysql/conf.d/
etc/mysql/conf.d/mysql.cnf
etc/mysql/conf.d/mysqldump.cnf
etc/mysql/my.cnf.fallback
etc/mysql/my.cnf
etc/mysql/mariadb.cnf
etc/mysql/mariadb.conf.d/
etc/mysql/mariadb.conf.d/50-client.cnf
etc/mysql/mariadb.conf.d/50-mysql-clients.cnf
.....
...
..

The -i option force the tar command to ignore zeroed blocks in archive (EOF). This is needed if you are using method #2.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
5 comments… add one
  • al May 31, 2017 @ 14:41

    gzip -dc file.tar.gz | tar xvf -

  • Ren Yuntao Jun 1, 2017 @ 0:05

    Method #3:
    ls *tar.gz | xargs -n 1 tar -zxvf

  • benjamin Jun 2, 2017 @ 3:49

    Method #4:

    ls *.tar.gz | xargs -IH tar xvf H

  • selebets Aug 11, 2020 @ 8:19

    I very new with linux… i using 7zip at window to do unpack the tar file using this following batch script… How to convert it to use in linux bash? I’m noticed that linux dose not require 7zip..

    @ECHO ON
    set SEVENZIP_EXE=C:\Program Files\7-Zip\7z.exe
    set INPUT_FOLDER=D:\TAR3
    set OUTPUT_FOLDER=E:\OUT
    FOR /F "TOKENS=*" %%F IN ('DIR /S /B "%INPUT_FOLDER%\*.tar"') DO "%SEVENZIP_EXE%" x "%%~fF" -o"%OUTPUT_FOLDER%" %%~nF\JPG\
    PAUSE
    
    • 🐧 Vivek Gite Aug 11, 2020 @ 11:34

      Try:

      #!/bin/bash
      INPUT=/path/to/input_dir
      OUTPUT=/path/to/output_dir
      for f in ${INPUT}/*tar
      do 
        tar -xvf "$f" -C "${OUTPUT}"
      done
      

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.