{"id":4027,"date":"2018-05-22T17:17:51","date_gmt":"2018-05-22T13:17:51","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-tac-command\/"},"modified":"2018-05-22T17:17:51","modified_gmt":"2018-05-22T13:17:51","slug":"linux-tac-command-tutorial-for-beginners-with-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-tac-command-tutorial-for-beginners-with-examples\/","title":{"rendered":"Linux tac Command Tutorial for Beginners (with Examples)"},"content":{"rendered":"<p>We&#8217;ve already discussed the <a href=\"https:\/\/www.howtoforge.com\/linux-cat-command\/\" target=\"_blank\" rel=\"noopener noreferrer\">Linux cat command<\/a>\u00a0in one of our earlier tutorials. As you may be aware, the cat command is mainly used for displaying file contents in output. However, what you may not be aware of is that there exists a command that does exactly opposite of what cat does.<\/p>\n<p>The tool in question is <strong>tac<\/strong>, and in this tutorial, we will discuss its basics using some easy to understand examples. But before we do that, it&#8217;s worth mentioning that all examples in this article have been tested on an Ubuntu 16.04 LTS machine.<\/p>\n<h2 id=\"linux-tac-command\">Linux tac command<\/h2>\n<p>Tac is opposite to cat in the sense that the output it produces is presented in a way that the last line is displayed first, then the second last line, and so on. Following is the tool&#8217;s syntax:<\/p>\n<p class=\"command\">tac [OPTION]&#8230; [FILE]&#8230;<\/p>\n<p>And here&#8217;s what tac&#8217;s man page says about it:<\/p>\n<pre>Write each FILE to standard output, last line first. With no FILE, or when FILE is -, read <br\/>standard input.<\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a good idea on how the tool works.<\/p>\n<h2 id=\"q-how-to-use-tac\">Q1. How to use tac?<\/h2>\n<p>Simple, just run the &#8216;tac&#8217; command with a filename as input.<\/p>\n<p class=\"command\">tac [filename]<\/p>\n<p>For example:<\/p>\n<p class=\"command\">tac file2<\/p>\n<p><a class=\"fancybox\" id=\"img-tac-basic-usage\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/tac-basic-usage.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-tac-command-tutorial-for-beginners-with-examples.png\" alt=\"How to use tac\" width=\"250\" height=\"172\" title=\"\"><\/a><\/p>\n<p>So you can see the output produced by tac is exactly opposite of what cat produced.<\/p>\n<h2 id=\"q-how-to-make-tac-use-custom-separator-not-newline\">Q2. How to make tac use custom separator (not newline)?<\/h2>\n<p>For this, you&#8217;ll have to use the -s option. For example, the following command<\/p>\n<p class=\"command\">echo &#8220;1,2&#8221; | tac<\/p>\n<p>Produced this output:<\/p>\n<pre>1,2<\/pre>\n<p>That&#8217;s because newline is the default separator for tac.<\/p>\n<p>However, the command below:<\/p>\n<p class=\"command\">echo &#8220;1,2&#8221; | tac -s ,<\/p>\n<p>produces the following output:<\/p>\n<pre>2<br\/>1,<\/pre>\n<p>That&#8217;s because now the separator has been changed.<\/p>\n<h2 id=\"q-how-to-change-the-position-of-separator\">Q3. How to change the position of separator?<\/h2>\n<p>If you want tac to attach the separator before instead of after, use the -b option.<\/p>\n<p>For example:<\/p>\n<p class=\"command\">echo &#8220;1,2&#8221; | tac -b -s ,<\/p>\n<p>produces the following output:<\/p>\n<pre>,2<br\/>1<\/pre>\n<h2 id=\"q-how-to-make-tac-accept-input-from-stdin\">Q4. How to make tac accept input from STDIN?<\/h2>\n<p>In case you want the tac command to accept input from the standard input, just don&#8217;t pass any file name to it.<\/p>\n<p class=\"command\">tac<\/p>\n<p>Once you run the command in the way mentioned above, it will wait for you to input content. Once you are done with that, press Ctrl+d to signify you&#8217;re done, and then tac will produce its output on STDOUT.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>As you can see, tac is easy to understand and work with. The number of command line options it offers is limited, and we&#8217;ve discussed most of them here. You can learn more about the command by heading to its <a href=\"https:\/\/linux.die.net\/man\/1\/tac\" 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-tac-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-tac-command-tutorial-for-beginners-with-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-tac-command%2F&amp;text=Linux+tac+Command+Tutorial+for+Beginners+%28with+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\/05\/linux-tac-command-tutorial-for-beginners-with-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\/05\/linux-tac-command-tutorial-for-beginners-with-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-tac-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-tac-command-tutorial-for-beginners-with-examples-4.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;ve already discussed the Linux cat command\u00a0in one of our earlier tutorials. As you may be aware, the cat command is mainly used for displaying file contents in output. However, what you may not be aware of is that there exists a command that does exactly opposite of what cat does. The tool in question &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-4027","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/4027","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=4027"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/4027\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=4027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=4027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=4027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}