[Linux](EN) Count number of bytes, characters, words and lines by using wc command
Let’s count number of bytes, characters, words and lines in file or input.
Environment and Prerequisite
- Linux base system
- Bash shell(/bin/bash)
wc command
What is wc command?
wc [OPTION]... [FILE]...
wc [OPTION]... --files0-from=F
wc
: print newline, word, and byte counts for each file or inputwc
stands for ‘word count’- You can use it with
ls -al
when count number of files and directories in specific path
Usage with each option
Make sample files for examples
- ASCII sample
$ cat << EOF > test_ascii.txt
123
abc def hij
lmn opq rs
EOF
$ file test_ascii.txt
test_ascii.txt: ASCII text
- UTF-8 sample
$ cat << EOF > test_utf.txt
한국어 배워보세요.
감사합니다. 또 봐요!
EOF
$ file test_utf.txt
test_utf.txt: UTF-8 Unicode text
-c, –bytes option
- print the byte counts.
$ wc -c test_ascii.txt
27 test_ascii.txt
# You can use it with cat command
$ cat test_ascii.txt | wc -c
27
-m, –chars option
- print the character counts.
$ wc -m test_ascii.txt
27 test_ascii.txt
# You can use it with cat command
$ cat test_ascii.txt | wc -m
27
- In ASCII format, the number of bytes and characters are same but some of other formats would be not.
$ wc -m test_ascii.txt
27 test_ascii.txt
$ wc -c test_ascii.txt
27 test_ascii.txt
$ wc -m test_utf.txt
24 test_utf.txt
$ wc -c test_utf.txt
56 test_utf.txt
-l, –lines option
- print the newline counts.
$ wc -l test_ascii.txt
3 test_ascii.txt
$ cat test_ascii.txt | wc -l
3
- You can count number of files and directories in specific path by using it with
ls -al
.
$ ls -al | wc -l
9
-w, –words option
- print the word counts.
$ wc -w test_ascii.txt
7 test_ascii.txt
$ wc -w test_utf.txt
5 test_utf.txt