How to extract tar.xz files in Linux and unzip all files

How do I extract tar.xz files in Linux? Can you tell me command to unzip linux-5.6.13.tar.xz file? How can I uncompressing (or decompressing) all files in the current directory?

The tar command and xz command provides support for extracting and uncompressing tar.xz files and .txz archives under Linux operating systems.

How do I extract tar.xz files in Linux?

The syntax is:

  1. Install xz using the dnf install xz on a CentOS/RHEL/Fedora Linux.
  2. Debian/Ubuntu Linux users try apt install xz-utils command.
  3. Extract tar.xz using the tar -xf backup.tar.xz command
  4. To decompress filename.tar.xz file run: xz -d -v filename.tar.xz

Installation

XZ is a set of open-source software for lossless data compressors, including LZMA and xz formats. These formats are popular among open source developers and projects due to higher compression rates than alternatives tools like gzip and bzip2. The tar command works if xz installed on the system. Hence, we must install it.

Debin/Ubuntu Linux install xz

Use the apt command/apt-get command:
$ sudo apt install xz-utils
Sample outputs:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 5 not upgraded.
Need to get 82.5 kB of archives.
After this operation, 0 B of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 xz-utils amd64 5.2.4-1 [82.5 kB]
Fetched 82.5 kB in 1s (60.3 kB/s)   
(Reading database ... 284817 files and directories currently installed.)
Preparing to unpack .../xz-utils_5.2.4-1_amd64.deb ...
Unpacking xz-utils (5.2.4-1) over (5.2.4-1) ...
Setting up xz-utils (5.2.4-1) ...
Processing triggers for man-db (2.9.1-1) ...

CentOS/RHEL/Fedora Linux install xz

Open the terminal app and then run NA command/dnf command as follows:
$ sudo dnf install xz
## On an older version of CentOS/RHEL try yum ##
$ sudo yum install xz

From my CentOS 6.x box:

Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: centos.excellmedia.net
 * extras: centos.excellmedia.net
 * updates: centos.excellmedia.net
Resolving Dependencies
--> Running transaction check
---> Package xz.x86_64 0:4.999.9-0.5.beta.20091007git.el6 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
=================================================================================
 Package  Arch         Version                                  Repository  Size
=================================================================================
Installing:
 xz       x86_64       4.999.9-0.5.beta.20091007git.el6         base       137 k
 
Transaction Summary
=================================================================================
Install       1 Package(s)
 
Total download size: 137 k
Installed size: 477 k
Is this ok [y/N]: y
Downloading Packages:
xz-4.999.9-0.5.beta.20091007git.el6.x86_64.rpm            | 137 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : xz-4.999.9-0.5.beta.20091007git.el6.x86_64                    1/1 
  Verifying  : xz-4.999.9-0.5.beta.20091007git.el6.x86_64                    1/1 
 
Installed:
  xz.x86_64 0:4.999.9-0.5.beta.20091007git.el6                                   
 
Complete!

Installing xz on OpenSUSE/SUSE Enterprise Linux

We can use the zypper command to install xz:
$ sudo zypper install xz

OpenSUSE installing xz package for extraction purposes

Examples

Now we install xz. We can now directly use the tar command as follows for extracting a file named linux-5.6.13.tar.xz:
$ tar -xf linux-5.6.13.tar.xz
$ tar -xvf linux-5.6.13.tar.xz
$ tar -Jxvf linux-5.6.13.tar.xz
## extract tar.xz files aka .txz file ##
$ tar --xz -xf archive.txz


Extracting specific files from a tar.xz file

We extract a given file names from a backup.tar.xz file by using the following syntax:
$ tar -xf backup.tar.xz resume.pdf
$ tar -xf backup.tar.xz babys-3rd-cake-day.jpg sales.txt

Sometimes we don’t know file names in advance. Hence, we list the contents of an archive by passing the -t as follows and then unzip tar.xz files:
$ tar -tvf backup.tar.xz
## filter out tar listing option using the grep command/egrep command ##
$ tar -tvf backup.tar.xz | grep file1
$ tar -tvf backup.tar.xz | egrep 'file.txt|data.doc|cake.jpg'


Sample outputs:

drwxrwxr-x root/root         0 2020-05-14 11:29 linux-5.6.13/
-rw-rw-r-- root/root     15844 2020-05-14 11:29 linux-5.6.13/.clang-format
-rw-rw-r-- root/root        59 2020-05-14 11:29 linux-5.6.13/.cocciconfig
-rw-rw-r-- root/root        71 2020-05-14 11:29 linux-5.6.13/.get_maintainer.ignore
-rw-rw-r-- root/root        62 2020-05-14 11:29 linux-5.6.13/.gitattributes
-rw-rw-r-- root/root      1746 2020-05-14 11:29 linux-5.6.13/.gitignore
-rw-rw-r-- root/root     15266 2020-05-14 11:29 linux-5.6.13/.mailmap
-rw-rw-r-- root/root       496 2020-05-14 11:29 linux-5.6.13/COPYING
-rw-rw-r-- root/root     99724 2020-05-14 11:29 linux-5.6.13/CREDITS
drwxrwxr-x root/root         0 2020-05-14 11:29 linux-5.6.13/Documentation/
-rw-rw-r-- root/root        13 2020-05-14 11:29 linux-5.6.13/Documentation/.gitignore
drwxrwxr-x root/root         0 2020-05-14 11:29 linux-5.6.13/Documentation/ABI/
-rw-rw-r-- root/root      3821 2020-05-14 11:29 linux-5.6.13/Documentation/ABI/README

Want to extract all Perl or Python source code files? Try the following option:
$ tar --wildcards '*.py' -xvf webroot.tar.xz
$ tar --wildcards '*.pl' -xvf centos-7-sysadmin-scripts.txz

Understanding tar command options

  • -x : Extract/get/unzip files from an archive.
  • -f archive.tar.xz : Use this archive file or device archive for extracting files
  • -J OR --xz : Filter the archive through xz command. Hence, we install xz using package manager.
  • -v : Verbose. Show progress.
  • -t : List file stored inside .tar.xz/.xz archive.
  • --wildcards : By default, wildcards don’t work as they treated or processed to by your current shell. Therefore, to extract files, use the --wildcards options. For example, extract all .webp images I would pass --wildcards '*.webp' option to the tar.

How to extract .xz files using the xz command

So far, we learned to use tar for extracting. But, we can use xz command directly for as compressor and decompressor too. Tar internally calls xz itself or using API provided by libs. Let us see some tips and tricks for xz command.

Decompress .tar.xz file

$ xz --decompress filename.tar.xz
$ xz -v -d archives.tar.xz

Decompress file named mysql.sql.xz and replaced by mysql.sql:
$ xz -v -d mysql.sql.xz

Compress tarball as .tar.xz file

Compress a file named mysql.sql and replace it by mysql.sql.xz for saving disk space:
$ xz mysql.sql
The syntax is as follows for compressing per-existing tarball:
$ xz -v -z filename.tar
Alternatively, use the tar command for convenience purposes
$ tar -cvJf {your-filename-here.tar.xz} file1 file2
$ tar -cvJf {your-filename-here.tar.xz} /path/to/dir1 ./dir2
$ sudo tar -cvJf /efs/backups/14-05-2020/production-webroot.tar.xz /var/www/
$ sudo tar --xz -cvf /efs/backups/aws-us-west-www-prod-42-etc.txz /etc/

Gif 01. xz in action

Compress from stdin

$ mysqldump nixcraft-blog | xz > nixcraft-blog.sql.xz
Of coruse we can uncompress and send content to stdout for the mysql command too
$ xzcat nixcraft-blog.sql.xz | mysql nixcraft-blog
Let us uncompress tar directory from stdin sent from the nc, enter (assuming that target server-ip protected by VPN else use the ssh command):
$ nc -l -p 4242 | tar -xJvf -
On other side compress directory to stdout and send to the nc, run:
$ tar -cJvz - /var/www/html | nc server-ip-here 4242
Our final example use ssh instead of nc:
$ tar --xz -cf - /path/to/dir | ssh sisrv@sisrv.net "cat > filename.txz"

Conclusion

This quick tutorial explained how to extract and unzip popular archive formats such as .tar.xz and .txz under Linux operating systems. Remember, tar command only works if you install the xz command via the package manager.

  • 1 Benutzer fanden dies hilfreich
War diese Antwort hilfreich?

Verwandte Artikel

SSH login without password

You want to use Linux and OpenSSH to automate your tasks. Therefore you need an automatic login...

How to remove a Linux user

How do I drop or remove old users account from my Linux server? I can login using the user’s SSH...

How to restrict and except SSH access to specific IPs

Once your IP is public it gets attention from so many bots in the internet that do brute force...

How to lock and unlock user account in linux

With the help of two commands you can lock and unlock the user account in Linux. To Lock the...

Adding a user to a group

Synopsis This article describes how to add a Linux user (account) to a group. Environment...

Powered by WHMCompleteSolution