{"id":7180,"date":"2018-10-31T12:00:02","date_gmt":"2018-10-31T09:00:02","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-xz-command\/"},"modified":"2018-10-31T12:00:02","modified_gmt":"2018-10-31T09:00:02","slug":"linux-xz-command-tutorial-for-beginners-7-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-xz-command-tutorial-for-beginners-7-examples\/","title":{"rendered":"Linux xz Command Tutorial for Beginners (7 Examples)"},"content":{"rendered":"<p>There are several ways to compress and decompress files in Linux. While we have already discussed some (<a href=\"https:\/\/www.howtoforge.com\/linux-gzip-command\/\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a> and <a href=\"https:\/\/www.howtoforge.com\/linux-gunzip-command\/\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>), there are plenty that we still haven&#8217;t. So here, in this tutorial, we will discuss another such command line utility.<\/p>\n<p>It&#8217;s dubbed <strong>xz<\/strong>. We will discuss the basics of this command line tool. But before we jump into that, it&#8217;s worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.<\/p>\n<h2 id=\"linux-xz-command\">Linux xz command<\/h2>\n<p>As already mentioned in the beginning, the xz command in Linux lets you compress and decompress files. Following is its syntax:<\/p>\n<p class=\"command\">xz [option&#8230;]\u00a0 [file&#8230;]<\/p>\n<p>And here&#8217;s what the man page has to say about it:<\/p>\n<pre readability=\"10\"> xz is a general-purpose data compression tool with command line syntax similar to gzip(1) <br\/>and bzip2(1).\u00a0 The native file format is the .xz format, but the legacy .lzma format used <br\/>by LZMA Utils and raw compressed streams with no container format headers are also supported.<p>\u00a0\u00a0\u00a0\u00a0 xz compresses or decompresses each file according to the selected operation mode.\u00a0 If no files<br\/>are given or file\u00a0 is\u00a0 -,\u00a0 xz\u00a0 reads from standard input and writes the processed data to <br\/>standard output.\u00a0 xz will refuse (display an error and skip the file) to write compressed data<br\/>to standard output if it is a terminal.\u00a0 Similarly, xz will refuse to read compressed data <br\/>from standard\u00a0 input if it is a terminal.<\/p><\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a better idea on how xz works.<\/p>\n<h2 id=\"q-how-to-use-the-xz-command\">Q1. How to use the xz command?<\/h2>\n<p>Basic usage is fairly straight forward. Just pass the name of the file (that&#8217;s to be compressed) as an input argument to xz. Here&#8217;s an example:<\/p>\n<p class=\"command\">xz file.txt<\/p>\n<p>On my system, the aforementioned command produced the following file as output.<\/p>\n<pre>file.txt.xz<\/pre>\n<p>So you can see the xz command compressed file.txt. It&#8217;s worth mentioning that the operation replaces the original file (file.txt in this case) with the compressed version.<\/p>\n<h2 id=\"q-how-to-make-xz-retain-original-file-as-well\">Q2. How to make xz retain original file as well?<\/h2>\n<p>As I mentioned in the previous section, the xz command replaces the original file with its compressed version. However, if you want, you can force xz to retain the original file as well. This can be done using the -k command line option.<\/p>\n<p>For example:<\/p>\n<p class=\"command\">xz -k file.txt<\/p>\n<p>So this time, you&#8217;ll find both file.txt and file.txt.xz in the current working directory.<\/p>\n<h2 id=\"q-how-to-compress-multiple-files\">Q3. How to compress multiple files?<\/h2>\n<p>This is very simple. Just pass the names as input arguments to xz.<\/p>\n<p>For example:<\/p>\n<p class=\"command\">xz file1.txt file2.txt<\/p>\n<p>This command will compress both these files in one go.<\/p>\n<h2 id=\"q-how-to-decompress-xz-files\">Q4. How to decompress .xz files?<\/h2>\n<p>To decompress .xz files, use the -d command line option. For example:<\/p>\n<p class=\"command\">xz -d file.txt.xz<\/p>\n<p>This command would produce file.txt in the current working directory.<\/p>\n<h2 id=\"q-how-to-make-xz-print-information-about-compressed-files\">Q5. How to make xz print information about compressed files?<\/h2>\n<p>This can be done using the -l command line option. For example:<\/p>\n<p class=\"command\">xz -l file.txt.xz<\/p>\n<p>This command produced the following information in my case:<\/p>\n<pre>Strms\u00a0 Blocks\u00a0\u00a0 Compressed Uncompressed\u00a0 Ratio\u00a0 Check\u00a0\u00a0 Filename<br\/>\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 96 B\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 37 B\u00a0 2.595\u00a0 CRC64\u00a0\u00a0 file.txt.xz<\/pre>\n<h2 id=\"q-how-to-specify-a-different-compressiondecompression-format\">Q6. How to specify a different compression\/decompression format?<\/h2>\n<p>This can be done using the -F command line option. To use this option though, you need to understand the following:<\/p>\n<pre readability=\"26\">-F format, --format=format<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Specify the file format to compress or decompress:<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 auto\u00a0\u00a0 This is the default.\u00a0 When compressing, auto is equivalent to xz.\u00a0 When decompressing, the format of the\u00a0 input\u00a0 file<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 is automatically detected.\u00a0 Note that raw streams (created with --format=raw) cannot be auto-detected.<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 xz\u00a0\u00a0\u00a0\u00a0 Compress to the .xz file format, or accept only .xz files when decompressing.<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lzma, alone<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Compress\u00a0 to the legacy .lzma file format, or accept only .lzma files when decompressing.\u00a0 The alternative name alone<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 is provided for backwards compatibility with LZMA Utils.<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 raw\u00a0\u00a0\u00a0 Compress or uncompress a raw stream (no headers).\u00a0 This is meant for advanced users only.\u00a0 To decode raw streams, you<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 need use --format=raw and explicitly specify the filter chain, which normally would have been stored in the container<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 headers.<\/p><\/pre>\n<p>So you see, you can use any of the following formats: &#8216;auto&#8217;, &#8216;xz&#8217;, &#8216;lzma&#8217;, and &#8216;raw&#8217;.<\/p>\n<h2 id=\"q-how-to-make-xz-display-progress-indicator\">Q7. How to make xz display progress indicator?<\/h2>\n<p>This can be made possible using the -v command line option. Here&#8217;s an example:<\/p>\n<p><a class=\"fancybox\" id=\"img-xz-v-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/xz-v-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/11\/linux-xz-command-tutorial-for-beginners-7-examples.png\" alt=\"How to make xz display progress indicator\" width=\"410\" height=\"60\" title=\"\"><\/a><\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>While we&#8217;ve discussed a handful of xz command line options in this tutorial, there are plenty more. Once you are done understanding and practicing these, head to the tool&#8217;s <a href=\"https:\/\/linux.die.net\/man\/1\/xz\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a> to learn more about it.<\/p>\n<div class=\"authorbox\" readability=\"15\">\n<img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/11\/linux-xz-command-tutorial-for-beginners-7-examples.jpg\" alt=\"Himanshu Arora\" title=\"\"> <\/p>\n<p><strong>About Himanshu Arora<\/strong><\/p>\n<p>Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu&#8217;s work has also been featured in some of world&#8217;s other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.<\/p>\n<\/div>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Flinux-xz-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/11\/linux-xz-command-tutorial-for-beginners-7-examples-1.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-xz-command%2F&amp;text=Linux+xz+Command+Tutorial+for+Beginners+%287+Examples%29&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/11\/linux-xz-command-tutorial-for-beginners-7-examples-2.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/11\/linux-xz-command-tutorial-for-beginners-7-examples-3.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-xz-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/11\/linux-xz-command-tutorial-for-beginners-7-examples-4.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are several ways to compress and decompress files in Linux. While we have already discussed some (here and here), there are plenty that we still haven&#8217;t. So here, in this tutorial, we will discuss another such command line utility. It&#8217;s dubbed xz. We will discuss the basics of this command line tool. But before [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-7180","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=7180"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7180\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=7180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=7180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=7180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}