{"id":2888,"date":"2018-02-16T19:04:07","date_gmt":"2018-02-16T16:04:07","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-stat-command\/"},"modified":"2018-02-16T19:04:07","modified_gmt":"2018-02-16T16:04:07","slug":"linux-stat-command-tutorial-for-beginners-5-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-stat-command-tutorial-for-beginners-5-examples\/","title":{"rendered":"Linux stat Command Tutorial for Beginners (5 Examples)"},"content":{"rendered":"<p>Sometimes, while working on the command line in Linux, you need to know more about a file. For example, you may want information like file&#8217;s size, inode number, access permissions, time of last access or modification, and more. You&#8217;ll be glad to know that there exists a command line utility <strong>stat<\/strong> that provides all this information in one go.<\/p>\n<p>In this tutorial, we will discuss the basics of stat using some easy to understand examples. But before we do that, it&#8217;s worth mentioning that all examples mentioned here have been tested on Ubuntu 16.04LTS.<\/p>\n<h2 id=\"linux-stat-command\">Linux stat command<\/h2>\n<p>The stat command in Linux allows you to view the status of a file (or even a file system). Here&#8217;s the syntax of the command:<\/p>\n<p class=\"command\">stat [OPTION]&#8230; FILE&#8230;<\/p>\n<p>Following are some Q&amp;A-styled examples that will give you a good idea on how stat works.<\/p>\n<h2 id=\"q-how-to-use-stat-command\">Q1. How to use stat command?<\/h2>\n<p>The tool&#8217;s basic usage is very easy &#8211; all you have to do is to run the &#8216;stat&#8217; command with the name of the file you want to know more about.<\/p>\n<p class=\"command\">stat [filename]<\/p>\n<p>For example:<\/p>\n<p class=\"command\">stat test.txt<\/p>\n<p><a class=\"fancybox\" id=\"img-stat-basic\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/stat-basic.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-stat-command-tutorial-for-beginners-5-examples.png\" alt=\"How to use stat command\" width=\"500\" height=\"136\" title=\"\"><\/a><\/p>\n<p>So you can see details like file name, size, type of file, access permissions, UIDs and GIDs, as well as Access\/Modify\/Change times.<\/p>\n<h2 id=\"q-how-to-make-stat-display-information-in-terse-form\">Q2. How to make stat display information in terse form?<\/h2>\n<p>The terse form of output makes it easy for other programs to parse the output. To make &#8216;stat&#8217; output information in this form, use the <strong>-t<\/strong> command line option.<\/p>\n<p class=\"command\">stat -t test.txt<\/p>\n<p><a class=\"fancybox\" id=\"img-stat-t-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/stat-t-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-stat-command-tutorial-for-beginners-5-examples-1.png\" alt=\"How to make stat display information in terse form\" width=\"550\" height=\"54\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-make-stat-output-info-in-custom-format\">Q3. How to make stat output info in custom format?<\/h2>\n<p>For this, use the <strong>&#8211;format<\/strong> option. This option requires you to enter a valid format sequence, and a lot of those are available. For example, to access permissions\/rights in human readable form, use <strong>%A<\/strong>.<\/p>\n<p>For example:<\/p>\n<p class=\"command\">stat &#8211;format=%A test.txt<\/p>\n<p>produces the following output:<\/p>\n<p><a class=\"fancybox\" id=\"img-stat-format-ex-1\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/stat-format-ex-1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-stat-command-tutorial-for-beginners-5-examples-2.png\" alt=\"How to make stat output info in custom format\" width=\"384\" height=\"52\" title=\"\"><\/a><\/p>\n<p>Similarly, you can use <strong>%n<\/strong> for file name, <strong>%s<\/strong> for size, and <strong>%U<\/strong> for user name of the file owner.<\/p>\n<p><a class=\"fancybox\" id=\"img-stat-more-formats\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/stat-more-formats.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/02\/linux-stat-command-tutorial-for-beginners-5-examples-3.png\" alt=\"Stat command output\" width=\"384\" height=\"102\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-is-format-option-different-from-printf\">Q4. How is &#8211;format option different from &#8211;printf?<\/h2>\n<p>At the core, both options do the same thing. The difference lies in the way outputs are produced. While <strong>&#8211;format<\/strong> outputs a newline after each use, in case of <strong>&#8211;printf<\/strong>, no newline is added. Here&#8217;s how the man page explains both:<\/p>\n<pre readability=\"10\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -c\u00a0 --format=FORMAT<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 use the specified FORMAT instead of the default; output\u00a0 a\u00a0 new?<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 line after each use of FORMAT<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 --printf=FORMAT<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 like\u00a0 --format, but interpret backslash escapes, and do not out?<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 put a mandatory trailing newline; if you want a newline, include<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\n in FORMAT<\/p><\/pre>\n<h2 id=\"q-how-to-make-sure-stat-dereferences-in-case-input-file-is-a-link\">Q5. How to make sure stat dereferences in case input file is a link?<\/h2>\n<p>There&#8217;s a dedicated option for this as well: <strong>-L<\/strong>. So if the input file is a link, and you want stat to follow it when the command runs, run it in the following way:<\/p>\n<p class=\"command\">stat -L [filename]<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>It&#8217;d be wrong to say <em>stat<\/em> doesn&#8217;t have a learning curve, but the amount of effort the tool demands isn&#8217;t much, as the number of command line options it offers are limited. We have already discussed the majority of them here. For more information, head to the tool&#8217;s <a href=\"https:\/\/linux.die.net\/man\/1\/stat\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a>.<\/p>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, while working on the command line in Linux, you need to know more about a file. For example, you may want information like file&#8217;s size, inode number, access permissions, time of last access or modification, and more. You&#8217;ll be glad to know that there exists a command line utility stat that provides all this [&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-2888","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2888","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=2888"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2888\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=2888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=2888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=2888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}