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 5781 file1.sh [root@nglinux]#
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 file1.sh 212 828 5781 file1.sh [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 file1.sh file2.sh 212 828 5781 file1.sh 141 528 4197 file2.sh 353 1356 9978 total [root@nglinux ~]#
Do post your comments or questions below.