{"id":3905,"date":"2018-05-17T18:27:20","date_gmt":"2018-05-17T14:27:20","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-tail-command\/"},"modified":"2018-05-17T18:27:20","modified_gmt":"2018-05-17T14:27:20","slug":"linux-tail-command-tutorial-for-beginners-5-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-tail-command-tutorial-for-beginners-5-examples\/","title":{"rendered":"Linux tail Command Tutorial for Beginners (5 Examples)"},"content":{"rendered":"<p>Sometimes you want to monitor what new information is being written to a file (think of log files), or for whatever reasons, want to access the last few lines of a file. Well, there&#8217;s a command line utility that lets you do this in Linux, and it&#8217;s call <strong>tail<\/strong>.<\/p>\n<p>In this tutorial, we will discuss the basics of the tail command using some easy to understand examples. But before we do that, it&#8217;s worth mentioning that all examples included in this article have been tested on Ubuntu 16.04 LTS.<\/p>\n<h2 id=\"linux-tail-command\">Linux tail command<\/h2>\n<p>As already mentioned, the tail command outputs the last part of files supplied to it as input. Following is its syntax:<\/p>\n<p class=\"system command\">tail [OPTION]&#8230; [FILE]&#8230;<\/p>\n<p>And here&#8217;s what the tool&#8217;s man page says about it:<\/p>\n<pre>Print the last 10 lines of each FILE to standard output. With more than one FILE, precede each with<br\/>a header giving the file name. With no FILE, or when FILE is -, read standard input.<\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a better idea on how the <strong>tail<\/strong> command works.<\/p>\n<h2 id=\"q-how-to-use-tail\">Q1. How to use tail?<\/h2>\n<p>Basic usage is very easy &#8211; all you have to do is to pass the name of the file whose last 10 lines you want the tool to display. For example:<\/p>\n<p class=\"command\">tail file1<\/p>\n<p>Following were the last 10 lines in file2 on my system:<\/p>\n<p><a class=\"fancybox\" id=\"img-tail-basic\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/tail-basic.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-tail-command-tutorial-for-beginners-5-examples.png\" alt=\"How to use tail command\" width=\"245\" height=\"186\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-change-number-of-lines-tail-prints\">Q2. How to change number of lines tail prints?<\/h2>\n<p>As you know by now, the tail command prints 10 lines by default. However, you can change this number using the -n command line option.<\/p>\n<p class=\"command\">tail -n [num] [filename]\n<p>For example:<\/p>\n<p class=\"command\">tail -n 5 file2<\/p>\n<p><a class=\"fancybox\" id=\"img-tail-n-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/tail-n-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-tail-command-tutorial-for-beginners-5-examples-1.png\" alt=\"How to change number of lines tail prints\" width=\"293\" height=\"103\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-make-tail-output-a-set-number-of-bytes\">Q3. How to make tail output a set number of bytes?<\/h2>\n<p>What if instead of lines, you want tail to deal in bytes. I mean, what if the requirement is to print 20 bytes? Well, this can be done using the -c command line option.<\/p>\n<p class=\"command\">tail -c [num] [filename]\n<p>For example:<\/p>\n<p class=\"command\">tail -c 20 file2<\/p>\n<p><a class=\"fancybox\" id=\"img-tail-c-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/tail-c-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-tail-command-tutorial-for-beginners-5-examples-2.png\" alt=\"How to make tail output a set number of bytes\" width=\"302\" height=\"102\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-make-tail-follow-log-files\">Q4. How to make tail follow log files?<\/h2>\n<p>If you want to use tail to follow updates to a file (say a log file) in real-time, use the -F or &#8211;follow command line option.<\/p>\n<p class=\"command\">tail -F [filename]\n<p>For example:<\/p>\n<p class=\"command\">tail -F file2<\/p>\n<p>Note that -f is equal to &#8216;&#8211;follow=name &#8211;retry&#8217;.<\/p>\n<p>Here&#8217;s some information the man page lists about the -f\/-F\/&#8211;follow options:<\/p>\n<pre>With --follow (-f), tail defaults to following the file descriptor, which means that even if a <br\/>tail'ed file is\u00a0 renamed,\u00a0 tail\u00a0 will continue\u00a0 to\u00a0 track\u00a0 its end.\u00a0 This default behavior is not <br\/>desirable when you really want to track the actual name of the file, not the file descriptor <br\/>(e.g., log rotation).\u00a0 Use --follow=name in that case. That causes tail to track the named file in <br\/>a way that accommodates renaming, removal and creation.<\/pre>\n<h2 id=\"q-how-to-force-tail-to-keep-trying-accessing-input-file\">Q5. How to force tail to keep trying accessing input file?<\/h2>\n<p>Sometimes a file may be temporarily inaccessible, something which can happen due to a variety of reasons. To make sure tail keeps trying opening the file, use the &#8211;retry option.<\/p>\n<p class=\"command\">tail &#8211;retry [filename]\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>So that&#8217;s how the tail command works. Hope you&#8217;d have enjoyed the tutorial, as well as learned useful stuff from it. We suggest you try out these examples on your Linux PC, and then head to the tool&#8217;s\u00a0<a href=\"https:\/\/linux.die.net\/man\/1\/tail\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a> for more info.<\/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-tail-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-tail-command-tutorial-for-beginners-5-examples-3.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-tail-command%2F&amp;text=Linux+tail+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\/05\/linux-tail-command-tutorial-for-beginners-5-examples-4.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-tail-command-tutorial-for-beginners-5-examples-5.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-tail-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-tail-command-tutorial-for-beginners-5-examples-6.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you want to monitor what new information is being written to a file (think of log files), or for whatever reasons, want to access the last few lines of a file. Well, there&#8217;s a command line utility that lets you do this in Linux, and it&#8217;s call tail. In this tutorial, we will discuss &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-3905","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/3905","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=3905"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/3905\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=3905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=3905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=3905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}