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?
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 including macOS, FreeBSD, OpenBSD, NetBSD and Linux distros. Let us see how to extract multiple tar ball files in a dirctory.
The problem with multiple tar ball files on Linux/Unix
Assuming that you have three files in the current directory as follows:
- conf.tar.gz
- mysql.tar.gz
- www.tar.gz
Let us verify it with the ls command
$ ls
Here are my tar balls:
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:
Method #1: Untar and extract multiple tar ball files using bash for loop
Use the bash for loop:
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 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 ..... ... ..
NOTE: The -i option force the tar command to ignore zeroed blocks in archive (EOF). This is needed if you are using method #2.
How To Extract a Tar Files To a Different Directory on a Linux/Unix-like Systems
Pass the -C dir option. For instance:
# Extract multiple tar ball files to /home/vivek/mydata/ directory # for f in *.tar.xz; do tar -xvfC /home/vivek/mydata/ "$f"; done # OR # cat *.tar.bz2 | tar jxvfc /home/vivek/mydata - -i
Understanding tar extracting CLI options
Where the tar command are as follows:
- z : Filter the archive through gzip command
- x : Extract option
- v : Verbose output. In other words, show progress while extracting files
- f : Filename to work on
- i : See note above.
- J : Filter for .xz file. See how to extract xz files for more info.
- j : Filter for .bz2 file
- - : Read from pipe or stdin
- C DIR_NAME : Cd in to the DIR_NAME before performing extract operations.
Wrapping up
We explained how to extract multiple tar ball files on Linux and Unix-like systems. See tar man page using the man command:
% man tar
% man bash
% help for
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 6 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
gzip -dc file.tar.gz | tar xvf -
Method #3:
ls *tar.gz | xargs -n 1 tar -zxvf
Method #4:
ls *.tar.gz | xargs -IH tar xvf H
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..
Try:
Thank you for the guide. It really helped us.
– OPENLitePanel Team