{"id":4579,"date":"2024-04-03T02:15:00","date_gmt":"2024-04-03T06:15:00","guid":{"rendered":"https:\/\/www.both.org\/?p=4579"},"modified":"2024-04-03T06:23:12","modified_gmt":"2024-04-03T10:23:12","slug":"getting-started-with-the-linux-cat-command","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=4579","title":{"rendered":"Getting started with the Linux cat command"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"4579\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">1    <\/span>\r\n<\/div><\/div>\n<p class=\"has-small-font-size\">Photo by Ruca Souza from Pexels<\/p>\n\n\n\n<p><strong>Cat<\/strong> is a fairly simple tool designed to concatenate and write file(s) to your screen, which is known as standard output (stdout). It is part of the <a href=\"https:\/\/www.both.org\/?p=4575\" data-type=\"link\" data-id=\"https:\/\/www.both.org\/?p=4575\" target=\"_blank\" rel=\"noreferrer noopener\">GNU Cor<\/a><a href=\"https:\/\/www.both.org\/?p=4575\" data-type=\"link\" data-id=\"https:\/\/www.both.org\/?p=4575\">e Utils<\/a> released under the GPLv3+ license. You can expect to find it in just about any Linux distribution or other Unix operating environment, such as FreeBSD or Solaris. The simplest use of&nbsp;<strong>cat<\/strong> is to show the contents of a file. Here is an example with a file named <em>hello.world<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ls\nhello.world\n$ cat hello.world\nHello World!\n<\/pre>\n\n\n\n<p>The most common way I use the <strong>cat<\/strong> command is for viewing configuration files, such as those in the <em>\/etc<\/em> directory. The <strong>cat<\/strong> command will display a file without risking damage to it. If I open a critical configuration file using an editor such as Vi or Nano, I could inadvertently make unwanted changes to the file. The <strong>cat<\/strong> command is not an editor and therefore poses no risk of making changes to a file&#8217;s content.<\/p>\n\n\n\n<p>If I need to view a longer file, I can use a pipe with the <strong>more<\/strong> command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cat &lt;somelongfile&gt; | more<\/pre>\n\n\n\n<p><strong>Cat<\/strong> can display multiple files at the same time. If we want to see two files\u2014<em>hello.world<\/em> and <em>goodbye.world<\/em>\u2014we would include both filenames as arguments in the command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cat hello.world goodbye.world\nHello World!\n\nGood Bye World!<\/pre>\n\n\n\n<p><strong>Cat<\/strong> can also number a file&#8217;s lines during output. There are two commands to do this, as shown in the help documentation:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">-b, --number-nonblank    number nonempty output lines, overrides -n\n-n, --number             number all output lines<\/pre>\n\n\n\n<p>If I use the <strong>-b<\/strong> command with the <em>hello.world<\/em> file, the output will be numbered like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cat -b hello.world\n     1\tHello World!<\/pre>\n\n\n\n<p>In the example above, there is an empty line. We can determine why this empty line appears by using the <strong>-n<\/strong> argument:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cat -n hello.world\n     1\tHello World!\n     2<\/pre>\n\n\n\n<p>Now we see that there is an extra empty line. These two arguments are operating on the final output rather than the file contents, so if we were to use the <strong>-n<\/strong> option with both files, numbering will count lines as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cat -n hello.world goodbye.world\n     1\tHello World!\n     2\t\n     3\tGood Bye World!\n     4<\/pre>\n\n\n\n<p>One other option that can be useful is <strong>-s<\/strong> for <em>squeeze-blank<\/em>. This argument tells <strong>cat<\/strong> to reduce repeated empty line output down to one line. This is helpful when reviewing files that have a lot of empty lines, because it effectively fits more text on the screen. Suppose I have a file with three lines that are spaced apart by several empty lines, such as in this example,&nbsp;<em>greetings.world<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cat greetings.world\nGreetings World!\n\n\n\n\nTake me to your Leader!\n\n\n\n\nWe Come in Peace!<\/pre>\n\n\n\n<p>Using the <strong>-s<\/strong> option saves screen space:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cat -s greetings.world\nGreetings World!\n\nTake me to your Leader!\n\nWe Come in Peace!<\/pre>\n\n\n\n<p><strong>Cat<\/strong> is often used to copy contents of one file to another file. You may be asking, &#8220;Why not just use <strong>cp<\/strong>?&#8221; Here is how I could create a new file, called <em>both.files<\/em>, that contains the contents of the <em>hello<\/em> and <em>goodbye<\/em> files:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cat hello.world goodbye.world &gt; both.files\n$ cat both.files\nHello World!\n\nGood Bye World!<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">zcat<\/h2>\n\n\n\n<p>There is another variation on the <strong>cat<\/strong> command known as <strong>zcat<\/strong>. This command is capable of displaying files that have been compressed with <a href=\"https:\/\/www.gnu.org\/software\/gzip\/\" target=\"_blank\" rel=\"noreferrer noopener\">Gzip<\/a> without needing to uncompress the files with the <strong>gunzip<\/strong> command. As an aside, this also preserves disk space, which is the entire reason files are compressed!<\/p>\n\n\n\n<p>The <strong>zcat<\/strong> command is a bit more exciting because it can be a huge time saver for system administrators who spend a lot of time reviewing system log files. Where can we find compressed log files? Take a look at <em>\/var\/log<\/em> on most Linux systems. On my system, <em>\/var\/log<\/em> contains several files, such as <em>syslog.2.gz<\/em> and <em>syslog.3.gz<\/em>. These files are the result of the log management system, which rotates and compresses log files to save disk space and prevent logs from growing to unmanageable file sizes. Without <strong>zcat<\/strong>, I would have to uncompress these files with the <strong>gunzip<\/strong> command before viewing them. Thankfully, I can use <strong>zcat<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cd \/var\/log\n$ ls *.gz\nsyslog.2.gz  syslog.3.gz\n$\n$ zcat syslog.2.gz |more\nJan 30 00:02:26 workstation systemd[1850]: Starting GNOME Terminal Server...\nJan 30 00:02:26 workstation dbus-daemon[1920]: [session uid=2112 pid=1920] Successful\nly activated service 'org.gnome.Terminal'\nJan 30 00:02:26 workstation systemd[1850]: Started GNOME Terminal Server.\nJan 30 00:02:26 workstation org.gnome.Terminal.desktop[2059]: # watch_fast: \"\/org\/gno\nme\/terminal\/legacy\/\" (establishing: 0, active: 0)\nJan 30 00:02:26 workstation org.gnome.Terminal.desktop[2059]: # unwatch_fast: \"\/org\/g\nnome\/terminal\/legacy\/\" (active: 0, establishing: 1)\nJan 30 00:02:26 workstation org.gnome.Terminal.desktop[2059]: # watch_established: \"\/\norg\/gnome\/terminal\/legacy\/\" (establishing: 0)\n--More--<\/pre>\n\n\n\n<p>We can also pass both files to <strong>zcat<\/strong> if we want to review both of them uninterrupted. Due to how log rotation works, you need to pass the filenames in reverse order to preserve the chronological order of the log contents:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ls -l *.gz\n-rw-r----- 1 syslog adm  196383 Jan 31 00:00 syslog.2.gz\n-rw-r----- 1 syslog adm 1137176 Jan 30 00:00 syslog.3.gz\n$ zcat syslog.3.gz syslog.2.gz |more<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Closing thoughts<\/h2>\n\n\n\n<p>The <strong>cat<\/strong> command seems simple but is very useful. I use it regularly. You also don&#8217;t need to feed or pet it like a real cat. As always, I suggest you review the man pages (<strong>man cat<\/strong>) for the <strong>cat<\/strong> and <strong>zcat<\/strong> commands to learn more about how it can be used. You can also use the <strong>&#8211;help<\/strong> argument for a quick synopsis of command line arguments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cat is a fairly simple tool designed to concatenate and write file(s) to your screen, which is known as standard output (stdout). The simplest use of\u00a0cat is to show the contents of a file.<\/p>\n","protected":false},"author":429,"featured_media":3469,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[5],"tags":[],"class_list":["post-4579","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/4579","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/429"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4579"}],"version-history":[{"count":5,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/4579\/revisions"}],"predecessor-version":[{"id":4749,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/4579\/revisions\/4749"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/3469"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}