How to get Byte Count in a File in Linux

|
Last Updated:
|
|

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 commonly consists of eight bits. File sizes are measured in bytes.

How to get byte count in a file in Linux

There are various Linux commands that you can use to get by count in a file.

Some of these commands include;

  • wc
  • stat
  • du
  • ls

So how can you use wc, stat, du, ls commands to get byte count in a file in Linux?

How to get byte count in a file using wc command

wc command is used to print newline, word, and byte counts for each file. The command line syntax is;

wc [OPTION]... [FILE]...

It has multiple command line options that can be passed to it for various functionalities.

However, in order to get the byte count in a file using wc command, you have to pass the -c/--bytes option.

wc -c NAME-of-FILE

E.g;

wc -c wp-config.php

Sample output;

3598 wp-config.php

If you want to print just the bytes without name of the file;

wc -c < FILE-NAME

E.g;

wc -c < wp-config.php

Sample output;

3598

You can also get a byte count for specific line number in a file;

awk '{if(NR==LINE_NUMBER) print $0}' FILENAME | wc -c
sed -n LINE_NUMBERp FILENAME | wc -c

Read more on man wc.

How to get byte count in a file using stat command

stat command is used to display file or file system status.

To get the byte count in a file using stat command, simply pass option -c FORMAT FILENAME/--format=FORMAT FILENAME.

You can get the formats from stat command man page (man stat).

The %s shows total size of the file in bytes.

stat -c %s FILENAME

or

stat --format=%s FILENAME

Example;

stat --format=%s wp-config.php

Sample output;

3598

Similarly, stat -c %s wp-config.php should give the same result.

How to get byte count in a file using du command

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.

To get byte count of a file using du command, just pass option -b/--bytes FILENAME command.

du [-b|--bytes] FILENAME

For example;

du -b wp-config.php
3598	wp-config.php

Read more man du.

How to get byte count in a file using ls command

You can also get byte count in a file using ls command. ls command is generally used to list information about the FILEs/directories.

To get by count, you can simply do long listing of a file and you should be able to get byte count.

ls -l wp-config.php

Sample output;

-rw-r--r-- 1 kifarunix kifarunix 3598 May 26 12:20 wp-config.php

5th column of the output shows byte count.

You can extract the byte count only by printing the 5th field.

ls -l wp-config.php | awk '{print $5}'
ls -l wp-config.php | cut -d' ' -f5

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.

Other Tutorials

Uncomment Lines in a File using SED in Linux

How to copy paste lines in vim

Make Permanent DNS Changes on resolv.conf in Linux

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
gen_too
Co-founder of Kifarunix.com, Linux Tips and Tutorials. Linux/Unix admin and author at Kifarunix.com.

Leave a Comment