Counts characters inside a file in Linux bash.

Today we will look how to count number of characters in a file in bash shell on Linux.

We need to count number of characters sometimes when we associate C program with our shell script to create buffers/contants of particular size.

To get the number of characters in a file, we can use wc command with -c option.

1. Count number of characters in a file
We can get the output using “wc -c” command.

[root@nglinux]# wc -c file1

2. Default wc command output for single file
We should also know the default wc command output. Lets see the command output.

### We can see three numbers in output.
[root@nglinux ~]# wc 
 212  828 5781
[root@nglinux ~]#

First three fields are as follows:
a. 212 : Are the newlines in the file.
b. 141 : Number of words in the file.
c. 5781 : Byte/character count for the file.

3. Default wc command output for multiple files
In case of multiple files, wc command also displays the total stats of each field.

[root@nglinux ~]# wc 
 212  828 5781
 141  528 4197
 353 1356 9978 total
[root@nglinux ~]# 

Do post your comments or questions below.

0 0 votes
Article Rating
Notify of

1 Comment
Oldest Most Voted
Inline Feedbacks
View all comments
8 months ago

Thanks for the article.
One important correction: the syntax for character count is wc -m.
-c is for bytes.