The Linux command wc followed by the -c flag (wc -c) can be used to count file bytes.

This tutorial explains how to count file bytes using both wc and du commands. After reading this tutorial, you will know how to easily count bytes, lines, and words in Linux.

How to count bytes in Linux using wc -c:

The syntax is pretty simple; the example below shows the wc -c command used to count the bytes of the file named linuxhintsignal.c.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image1-30.png" data-lazy- height="235" src="data:image/svg xml,” width=”756″>

As you can see in the output, the file has 106 bytes.

The command wc -c can also be used to count multiple file bytes, as shown in the example below in which bytes for linuxhintsignal.c and wp-downgrade.zip are counted.

wc -c linuxhintsignal.c wp-downgrade.zip

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image3-29.png" data-lazy- height="263" src="data:image/svg xml,” width=”980″>

As you can see, wc prints the bytes of each file and the total sum.

You also can implement the wildcard to count all files within a directory, as shown below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image2-30.png" data-lazy- height="263" src="data:image/svg xml,” width=”812″>

Using the wildcard, wc will print all files and their bytes count as shown above.

The wc command is useful for more purposes than counting bytes. This command is also useful to count lines, words, and characters within a file or multiple files.

If you run wc without flags, only followed by the file name, it will print 3 columns as shown in the image below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image5-22.png" data-lazy- height="263" src="data:image/svg xml,” width=”798″>

The first column (11) shows the number of lines. The second column (14) shows the word count, and the third column shows bytes.

Of course, you also can count each characteristic separately.

You need to add the -l flag to count the line number, as shown in the example below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image4-26.png" data-lazy- height="235" src="data:image/svg xml,” width=”840″>

As you can see, the file contains 11 lines.

The example below shows how to execute a wordcount using the wc command with the -w flag.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image7-15.png" data-lazy- height="207" src="data:image/svg xml,” width=”714″>

The following example shows how to implement a pipe to combine commands ls and wc to get the total files to count within a directory.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image6-18.png" data-lazy- height="179" src="data:image/svg xml,” width=”770″>

You can read all wc features by running:

Or

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image9-12.png" data-lazy- height="599" src="data:image/svg xml,” width=”1134″>

You also can visit the online man page at https://linux.die.net/man/1/wc.

How to count bytes in Linux using du:

The du command also can be used to count bytes. By running the du command followed by the -b flag and the file name, you can get the bytes count as shown in the following image.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image8-15.png" data-lazy- height="207" src="data:image/svg xml,” width=”812″>

As with the wc command, with the du command, you also can define multiple files to be counted, as shown in the screenshot below. The difference with the wc’s output is the du command won’t show the total sum.

du -b linuxhintsignal.c wp-downgrade.zip

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image12-9.png" data-lazy- height="207" src="data:image/svg xml,” width=”924″>

As said previously, the difference with wc is the output doesn’t show the total amount of bytes. To get the total sum using du, you need to add the -c flag, as shown below.

du -bc linuxhintsignal.c wp-downgrade.zip

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image10-11.png" data-lazy- height="207" src="data:image/svg xml,” width=”924″>

Now let’s say you don’t want the output in bytes but in a human-readable format; for this, you can implement the -h (human) flag.

du -h linuxhintsignal.c wp-downgrade.zip

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image11-10.png" data-lazy- height="207" src="data:image/svg xml,” width=”924″>

As you can see, the output now is human-friendly.

The command du can be used to print file sizes in any unit you want. The example below shows how to print the size in kilobytes using the -k flag.

du -k linuxhintsignal.c wp-downgrade.zip

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image13-6.png" data-lazy- height="207" src="data:image/svg xml,” width=”924″>

It is very important to clarify that the du command can’t provide the total amount of kilobytes or megabytes if the file size doesn’t match the exact unit; in such a case, the du command will return the closest value of the unit format you defined.

Also, you can print the size of files in megabyte units by using the -m flag, as shown below.

du -m remove.iso linuxhintsignal.c

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/image14-6.png" data-lazy- height="179" src="data:image/svg xml,” width=”868″>

As you can see, you have the result in megabytes, not the exact amount of megabyte but the closer to the real value in bytes.

Conclusion:

As you can see, counting bytes using the wc -c command is pretty simple. Linux offers different ways to achieve this task; this tutorial focused on the 2 most common commands to count files size in byte units. As you could see, there is no meaningful difference between wc and du commands when counting bytes. The only difference described in this tutorial (additionally to flags) is the total sum of bytes in the output. The wc command is also useful to be implemented with pipes and other commands showing additional information.

Thank you for reading this tutorial on the wc command to count bytes. Keep following us for more Linux tips and tutorials.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/linuxinstitute_icono-150×150.png614a53ef19c99.jpg" height="112" src="data:image/svg xml,” width=”112″>

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.