{"id":11108,"date":"2022-07-22T11:44:59","date_gmt":"2022-07-22T08:44:59","guid":{"rendered":"https:\/\/kifarunix.com\/?p=11108"},"modified":"2024-03-09T16:17:28","modified_gmt":"2024-03-09T13:17:28","slug":"how-to-get-byte-count-in-a-file-in-linux","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/how-to-get-byte-count-in-a-file-in-linux\/","title":{"rendered":"How to get Byte Count in a File in Linux"},"content":{"rendered":"\n<p>This simple tutorial will show you how to get byte count in a file in Linux. <a href=\"https:\/\/en.wikipedia.org\/wiki\/Byte\" target=\"_blank\" rel=\"noreferrer noopener\">Byte<\/a> is a unit of digital information that most commonly consists of eight bits. File sizes are measured in bytes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to get byte count in a file in Linux<\/h2>\n\n\n\n<p>There are various Linux commands that you can use to get by count in a file.<\/p>\n\n\n\n<p>Some of these commands include;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>wc<\/li>\n\n\n\n<li>stat<\/li>\n\n\n\n<li>du<\/li>\n\n\n\n<li>ls<\/li>\n<\/ul>\n\n\n\n<p>So how can you use wc, stat, du, ls commands to get byte count in a file in Linux?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to get byte count in a file using wc command<\/h2>\n\n\n\n<p>wc command is used to print newline, word, and byte counts for each file. The command line syntax is;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wc &#91;OPTION]... &#91;FILE]...<\/code><\/pre>\n\n\n\n<p>It has multiple command line options that can be passed to it for various functionalities.<\/p>\n\n\n\n<p>However, in order to get the byte count in a file using wc command, you have to pass the <strong><code>-c\/--bytes<\/code><\/strong> option.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wc -c NAME-of-FILE<\/code><\/pre>\n\n\n\n<p>E.g;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wc -c wp-config.php<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>3598 wp-config.php<\/code><\/pre>\n\n\n\n<p>If you want to print just the bytes without name of the file;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wc -c &lt; FILE-NAME<\/code><\/pre>\n\n\n\n<p>E.g;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wc -c &lt; wp-config.php<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>3598<\/code><\/pre>\n\n\n\n<p>You can also get a byte count for specific line number in a file;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>awk '{if(NR==LINE_NUMBER) print $0}' FILENAME | wc -c<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>sed -n LINE_NUMBERp FILENAME | wc -c<\/code><\/pre>\n\n\n\n<p>Read more on <code>man wc<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to get byte count in a file using stat command<\/h3>\n\n\n\n<p>stat command is used to display file or file system status.<\/p>\n\n\n\n<p>To get the byte count in a file using stat command, simply pass option <code>-c FORMAT FILENAME<\/code>\/<code>--format=FORMAT FILENAME<\/code>.<\/p>\n\n\n\n<p>You can get the formats from stat command man page (man stat).<\/p>\n\n\n\n<p>The <strong><code>%s<\/code><\/strong> shows total size of the file in bytes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stat -c %s FILENAME<\/code><\/pre>\n\n\n\n<p>or<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stat --format=%s FILENAME<\/code><\/pre>\n\n\n\n<p>Example;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stat --format=%s wp-config.php<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>3598<\/code><\/pre>\n\n\n\n<p>Similarly, <code><strong>stat -c %s wp-config.php<\/strong><\/code> should give the same result.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to get byte count in a file using du command<\/h3>\n\n\n\n<p>du, aka, disk usage command can also be used to get by count in a file in Linux. du command basically summarizes disk usage of the set of FILEs, recursively for directories.<\/p>\n\n\n\n<p>To get byte count of a file using du command, just pass option <code><strong>-b\/--bytes FILENAME<\/strong><\/code> command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>du &#91;-b|--bytes] FILENAME<\/code><\/pre>\n\n\n\n<p>For example;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>du -b wp-config.php<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>3598\twp-config.php<\/code><\/pre>\n\n\n\n<p>Read more <code>man du<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to get byte count in a file using ls command<\/h3>\n\n\n\n<p>You can also get byte count in a file using <code>ls<\/code> command. ls command is generally used to list information about the FILEs\/directories.<\/p>\n\n\n\n<p>To get by count, you can simply do long listing of a file and you should be able to get byte count.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -l wp-config.php<\/code><\/pre>\n\n\n\n<p>Sample output;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-rw-r--r-- 1 kifarunix kifarunix <strong>3598<\/strong> May 26 12:20 wp-config.php<\/code><\/pre>\n\n\n\n<p>5th column of the output shows byte count.<\/p>\n\n\n\n<p>You can extract the byte count only by printing the 5th field.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -l wp-config.php | awk '{print $5}'<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -l wp-config.php | cut -d' ' -f5<\/code><\/pre>\n\n\n\n<p>And that is how you can easily get byte count in a file in Linux. There are other multiple options you can explore to that regard.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Other Tutorials<\/h3>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/uncomment-lines-in-a-file-using-sed-in-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\">Uncomment Lines in a File using SED in Linux<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-copy-paste-lines-in-vim\/\">How to copy paste lines in vim<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/make-permanent-dns-changes-on-resolv-conf-in-linux\/\" target=\"_blank\" rel=\"noreferrer noopener\">Make Permanent DNS Changes on resolv.conf in Linux<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This simple tutorial will show you how to get byte count in a file in Linux. Byte is a unit of digital information that most<\/p>\n","protected":false},"author":3,"featured_media":13535,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,49],"tags":[3291,5589,5593,5586,5587,5592,5591,5588],"class_list":["post-11108","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","category-command-cheatsheets","tag-du-command","tag-get-file-size-in-byes-using-wc-command","tag-get-number-of-bytes-in-a-file","tag-how-to-get-byte-count-in-a-file","tag-linux-file-byte-count","tag-stat-command","tag-use-ls-command-to-get-byte-count-in-a-file","tag-way-to-get-file-size-in-bytes-in-the-shell","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/11108"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=11108"}],"version-history":[{"count":4,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/11108\/revisions"}],"predecessor-version":[{"id":20585,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/11108\/revisions\/20585"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/13535"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=11108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=11108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=11108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}