wc

Count the number of bytes, words, and lines of a file

Supplementary instructions

wc command counts the number of bytes, words, and lines in the specified file, and displays and outputs the statistical results. We can use the wc command to calculate the number of Bytes, words, or columns of a file. If the file name is not specified, or the file name is given as "-", the wc command will read data from the standard input device. wc also gives the total count of the specified files.

grammar

wc(option)(parameter)
wc [options]... [file]...
wc [options]... --files0-from=F

Options

-c # Count the number of bytes, or --bytes: display the number of Bytes.
-l # Count the number of lines, or --lines: display the number of columns.
-m # Count the number of characters, or --chars: display the number of characters.
-w # Count word count, or --words: display word count. A word is defined as a string of characters separated by whitespace, tab, or newline characters.
-L # Print the length of the longest line, or --max-line-length.
-help # Display help information.
--version #Display version information.

Parameters

File: List of files to be counted.

example

wc -l * # Count the number of lines and the total number of lines in all files in the current directory.
wc -l *.js # Count the number of lines and the total number of lines in all files with .js suffix in the current directory.
find . * | xargs wc -l # The number of lines and the total number of lines in all files in the current directory and subdirectories.

View the number of bytes, words, and lines of the file

wctest.txt
# Output results
7 8 70 test.txt
# Number of lines Number of words Number of bytes File name

How to use wc command to print only statistics but not file names

wc -l < test.txt
# Output results
7

Used to count the number of files in the current directory (excluding hidden files)

#To remove the TOTAL line
expr $(ls -l | wc -l) - 1
# Output results
8

Count the number of lines and the total number of lines in all files in the current directory

[root@centos7 ~]# wc -l *
       21 LICENSE
      270 README.md
wc: example: read: Is a directory
      785 lerna-debug.log
       25lerna.json
wc: node_modules: read: Is a directory
    23603 package-lock.json
       79 package.json
        3 renovate.json
    24786 total