The function of the wc (Word Count) command in the Linux system is to count the number of bytes, words, and lines in the specified file, and display and output the statistical results.
1. Command format:
wc [options] file...
2. Command function:
Count the number of bytes, words, and lines in the specified file, and display and output the statistical results. This command counts the number of bytes, words and lines in the specified file. If no filename is given, it is read from standard input. wc also gives the presidential count for the specified file.
3. Command parameters:
-c Count bytes.
-l Count lines.
-m count characters. This flag cannot be used with the -c flag.
-w Count word count. A word is defined as a string separated by whitespace, tab or newline characters.
-L Print the length of the longest line.
-help display help information
--version display version information
4. Example of use:
Example 1: View the number of bytes, words, and lines of a file
Order:
wc test.txt
output:
[root@localhost test]# cat test.txt hnlinux peida.cnblogs.com ubuntu ubuntu linux redhat Redhat linuxmint [root@localhost test]# wc test.txt 7 8 70 test.txt [root@localhost test]# wc -l test.txt 7 test.txt [root@localhost test]# wc -c test.txt 70 test.txt [root@localhost test]# wc -w test.txt 8 test.txt [root@localhost test]# wc -m test.txt 70 test.txt [root@localhost test]# wc -L test.txt 17 test.txt
illustrate:
7 8 70 test.txt
Lines Words Bytes Filename
Example 2: How to use the wc command to print only the statistics but not the file name
Order:
output:
[root@localhost test]# wc -l test.txt 7 test.txt [root@localhost test]# cat test.txt |wc -l 7[root@localhost test]#
illustrate:
Use pipes, which are especially useful when writing shell scripts.
Example 3: Used to count the number of files in the current directory
Order:
ls -l | wc -l
output:
[root@localhost test]# cd test6 [root@localhost test6]# ll Total 604 ---xr--r-- 1 root mail 302108 11-30 08:39 linklog.log ---xr--r-- 1 mail users 302108 11-30 08:39 log2012.log -rw-r--r-- 1 mail users 61 11-30 08:39 log2013.log -rw-r--r-- 1 root mail 0 11-30 08:39 log2014.log -rw-r--r-- 1 root mail 0 11-30 08:39 log2015.log -rw-r--r-- 1 root mail 0 11-30 08:39 log2016.log -rw-r--r-- 1 root mail 0 11-30 08:39 log2017.log [root@localhost test6]# ls -l | wc -l 8 [root@localhost test6]#
illustrate:
The current directory is included in the number