Command Linux

Linux Wc -C Option to Count Bytes

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.

wc -c linuxhintsignal.c

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

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.

wc -c *

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.

wc linuxhintsignal.c

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.

wc -l linuxhintsignal.c

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.

wc -w linuxhintsignal.c

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.

ls wcdirectory/ | wc -l

You can read all wc features by running:

wc –help

Or

man wc

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.

du -b linuxhintsignal.c

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

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

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

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

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

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.

Related Articles

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Back to top button