{"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
This simple tutorial will show you how to get byte count in a file in Linux. 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 There are various Linux commands that you can use to get by count in a file.<\/p>\n\n\n\n Some of these commands include;<\/p>\n\n\n\n So how can you use wc, stat, du, ls commands to get byte count in a file in Linux?<\/p>\n\n\n\n wc command is used to print newline, word, and byte counts for each file. The command line syntax is;<\/p>\n\n\n\n It has multiple command line options that can be passed to it for various functionalities.<\/p>\n\n\n\n However, in order to get the byte count in a file using wc command, you have to pass the E.g;<\/p>\n\n\n\n Sample output;<\/p>\n\n\n\n If you want to print just the bytes without name of the file;<\/p>\n\n\n\n E.g;<\/p>\n\n\n\n Sample output;<\/p>\n\n\n\n You can also get a byte count for specific line number in a file;<\/p>\n\n\n\n Read more on stat command is used to display file or file system status.<\/p>\n\n\n\n To get the byte count in a file using stat command, simply pass option You can get the formats from stat command man page (man stat).<\/p>\n\n\n\n The or<\/p>\n\n\n\n Example;<\/p>\n\n\n\n Sample output;<\/p>\n\n\n\n Similarly, 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 To get byte count of a file using du command, just pass option For example;<\/p>\n\n\n\n Read more You can also get byte count in a file using 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 Sample output;<\/p>\n\n\n\n 5th column of the output shows byte count.<\/p>\n\n\n\n You can extract the byte count only by printing the 5th field.<\/p>\n\n\n\n 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 Uncomment Lines in a File using SED in Linux<\/a><\/p>\n\n\n\nHow to get byte count in a file in Linux<\/h2>\n\n\n\n
\n
How to get byte count in a file using wc command<\/h2>\n\n\n\n
wc [OPTION]... [FILE]...<\/code><\/pre>\n\n\n\n
-c\/--bytes<\/code><\/strong> option.<\/p>\n\n\n\n
wc -c NAME-of-FILE<\/code><\/pre>\n\n\n\n
wc -c wp-config.php<\/code><\/pre>\n\n\n\n
3598 wp-config.php<\/code><\/pre>\n\n\n\n
wc -c < FILE-NAME<\/code><\/pre>\n\n\n\n
wc -c < wp-config.php<\/code><\/pre>\n\n\n\n
3598<\/code><\/pre>\n\n\n\n
awk '{if(NR==LINE_NUMBER) print $0}' FILENAME | wc -c<\/code><\/pre>\n\n\n\n
sed -n LINE_NUMBERp FILENAME | wc -c<\/code><\/pre>\n\n\n\n
man wc<\/code>.<\/p>\n\n\n\n
How to get byte count in a file using stat command<\/h3>\n\n\n\n
-c FORMAT FILENAME<\/code>\/
--format=FORMAT FILENAME<\/code>.<\/p>\n\n\n\n
%s<\/code><\/strong> shows total size of the file in bytes.<\/p>\n\n\n\n
stat -c %s FILENAME<\/code><\/pre>\n\n\n\n
stat --format=%s FILENAME<\/code><\/pre>\n\n\n\n
stat --format=%s wp-config.php<\/code><\/pre>\n\n\n\n
3598<\/code><\/pre>\n\n\n\n
stat -c %s wp-config.php<\/strong><\/code> should give the same result.<\/p>\n\n\n\n
How to get byte count in a file using du command<\/h3>\n\n\n\n
-b\/--bytes FILENAME<\/strong><\/code> command.<\/p>\n\n\n\n
du [-b|--bytes] FILENAME<\/code><\/pre>\n\n\n\n
du -b wp-config.php<\/code><\/pre>\n\n\n\n
3598\twp-config.php<\/code><\/pre>\n\n\n\n
man du<\/code>.<\/p>\n\n\n\n
How to get byte count in a file using ls command<\/h3>\n\n\n\n
ls<\/code> command. ls command is generally used to list information about the FILEs\/directories.<\/p>\n\n\n\n
ls -l wp-config.php<\/code><\/pre>\n\n\n\n
-rw-r--r-- 1 kifarunix kifarunix 3598<\/strong> May 26 12:20 wp-config.php<\/code><\/pre>\n\n\n\n
ls -l wp-config.php | awk '{print $5}'<\/code><\/pre>\n\n\n\n
ls -l wp-config.php | cut -d' ' -f5<\/code><\/pre>\n\n\n\n
Other Tutorials<\/h3>\n\n\n\n