{"id":5725,"date":"2018-07-24T19:20:01","date_gmt":"2018-07-24T15:20:01","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-zcat-command\/"},"modified":"2018-07-24T19:20:01","modified_gmt":"2018-07-24T15:20:01","slug":"linux-zcat-command-tutorial-for-beginners-5-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-zcat-command-tutorial-for-beginners-5-examples\/","title":{"rendered":"Linux zcat Command Tutorial for Beginners (5 Examples)"},"content":{"rendered":"<p>While compression is helpful in general as it allows you to save space on your system, command line users need to uncompress the file for several small tasks, like viewing what all the file contains. However,\u00a0 there exists a command line utility &#8211; dubbed <strong>zcat<\/strong> &#8211; that lets you take a peek inside compressed files without any additional effort.<\/p>\n<p>In this tutorial, we will discuss the basics of zcat using some easy to understand examples. Please note that all examples in this article have been tested on Ubuntu 16.04 LTS.<\/p>\n<h2 id=\"linux-zcat-command\">Linux zcat command<\/h2>\n<p>The zcat command lets you view contents of a compressed file. Following is its syntax:<\/p>\n<p class=\"command\">zcat FILE &#8230;.<\/p>\n<p>And here&#8217;s how the tool&#8217;s man page explains it:<\/p>\n<pre> zcat\u00a0 is\u00a0 identical\u00a0 to\u00a0 gunzip\u00a0 -c.\u00a0\u00a0 (On\u00a0 some\u00a0 systems,\u00a0 zcat may be<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 installed as gzcat to preserve the original link\u00a0 to\u00a0 compress.)\u00a0\u00a0 zcat<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 uncompresses either a list of files on the command line or its standard<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 input and writes the uncompressed data on standard output.<\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a good idea on how the zcat command works.<\/p>\n<h2 id=\"q-how-to-use-zcat\">Q1. How to use zcat?<\/h2>\n<p>Suppose you have a compressed text file, say <em>test2.txt.gz<\/em>. If you try viewing it contents using the standard <a href=\"https:\/\/www.howtoforge.com\/linux-cat-command\/\" target=\"_blank\" rel=\"noopener noreferrer\">cat command<\/a>, you&#8217;ll see a garbled output:<\/p>\n<p><a class=\"fancybox\" id=\"img-cat-garbled\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/cat-garbled.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-zcat-command-tutorial-for-beginners-5-examples.png\" alt=\"cat vs. zcat\" width=\"428\" height=\"67\" title=\"\"><\/a><\/p>\n<p>So in this case, you can use the zcat command. Basic usage is very simple &#8211; just replace cat with zcat.<\/p>\n<p class=\"command\">zcat test2.txt.gz<\/p>\n<p><a class=\"fancybox\" id=\"img-zcat-basic-usage\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/zcat-basic-usage.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-zcat-command-tutorial-for-beginners-5-examples-1.png\" alt=\"How to use zcat\" width=\"308\" height=\"59\" title=\"\"><\/a><\/p>\n<p>So you can see the content of the compressed file was displayed on standard output.<\/p>\n<h2 id=\"q-does-zcat-work-with-multiple-inputs\">Q2. Does zcat work with multiple inputs?<\/h2>\n<p>Yes, it does. All you have to do is to pass the names of compressed files as input.<\/p>\n<p>For example:<\/p>\n<p class=\"command\">zcat test.txt.gz test2.txt.gz<\/p>\n<p>Here&#8217;s the aforementioned command in action:<\/p>\n<p><a class=\"fancybox\" id=\"img-zcat-multiple-input\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/zcat-multiple-input.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-zcat-command-tutorial-for-beginners-5-examples-2.png\" alt=\"Does zcat work with multiple inputs\" width=\"418\" height=\"53\" title=\"\"><\/a><\/p>\n<h2 id=\"q-does-zcat-recognize-compressed-file-based-on-extension\">Q3. Does zcat recognize compressed file based on extension?<\/h2>\n<p>No. That&#8217;s not the case. Here&#8217;s what the man page says about this:<\/p>\n<pre>zcat will uncompress files that have the correct magic number whether they have a .gz suffix or not.<\/pre>\n<p>To verify this, we removed the .gz extension from one of the compressed files, and tried running zcat on it.<\/p>\n<p><a class=\"fancybox\" id=\"img-zcat-no-ext\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/zcat-no-ext.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-zcat-command-tutorial-for-beginners-5-examples-3.png\" alt=\"Does zcat recognize compressed file based on extension\" width=\"382\" height=\"56\" title=\"\"><\/a><\/p>\n<p>So you can see the tool was still able to learn that it&#8217;s a compressed file, and was able to display its content on STDOUT.<\/p>\n<h2 id=\"q-how-to-handle-pagination-while-using-zcat\">Q4. How to handle pagination while using zcat?<\/h2>\n<p>There are two ways to do this. Either you can use the standard\u00a0<a href=\"https:\/\/www.howtoforge.com\/linux-more-command\/\" target=\"_blank\" rel=\"noopener\">more<\/a> and less commands.<\/p>\n<p class=\"command\">zcat [FILENAME] | more<\/p>\n<p class=\"command\">zcat [FILENAME] | less<\/p>\n<p>Or, use the zmore and zless commands.<\/p>\n<p class=\"command\">zmore [FILENAME]<\/p>\n<p class=\"command\">zless [FILENAME]<\/p>\n<h2 id=\"q-does-zcat-work-with-noncompressed-files\">Q5. Does zcat work with non-compressed files?<\/h2>\n<p>By default no. If you try running the zcat command with a non compressed file as input, you&#8217;ll see an error like following.<\/p>\n<p><a class=\"fancybox\" id=\"img-zcat-non-compressed\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/zcat-non-compressed.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-zcat-command-tutorial-for-beginners-5-examples-4.png\" alt=\"Does zcat work with non-compressed files\" width=\"312\" height=\"52\" title=\"\"><\/a><\/p>\n<p>However, if you want to force zcat to display file contents in output whether or not the file is compressed, then use the -f command line option. The following screenshot shows this option in action:<\/p>\n<p><a class=\"fancybox\" id=\"img-zcat-f\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/zcat-f.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-zcat-command-tutorial-for-beginners-5-examples-5.png\" alt=\"Zcat -f option\" width=\"299\" height=\"33\" title=\"\"><\/a><\/p>\n<p>So you can see the tool didn&#8217;t throw any error this time around.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Undoubtedly, the zcat command saves you time, if all you want is to take a look at the contents of a compressed file. So, regardless of what kind of Linux command line user you are (newbie or pro), you should learn about this command. We&#8217;ve already discussed some useful details related to zcat here. For more, head to its <a href=\"https:\/\/linux.die.net\/man\/1\/zcat\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a>.<\/p>\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-zcat-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-zcat-command-tutorial-for-beginners-5-examples-6.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-zcat-command%2F&amp;text=Linux+zcat+Command+Tutorial+for+Beginners+%285+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\/07\/linux-zcat-command-tutorial-for-beginners-5-examples-7.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\/07\/linux-zcat-command-tutorial-for-beginners-5-examples-8.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-zcat-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/07\/linux-zcat-command-tutorial-for-beginners-5-examples-9.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>While compression is helpful in general as it allows you to save space on your system, command line users need to uncompress the file for several small tasks, like viewing what all the file contains. However,\u00a0 there exists a command line utility &#8211; dubbed zcat &#8211; that lets you take a peek inside compressed files [&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-5725","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/5725","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=5725"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/5725\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=5725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=5725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=5725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}