The Linux wc command is used to count the number of lines, words, bytes and characters in a file.
Linux wc command Function Description
Use the wc command to count the number of lines, words, bytes and characters in a specified file and display the statistics output to the screen. If no file name is given, it is read from standard input.
wc also gives the president count of all specified files. Words are the largest strings distinguished by space characters. The order and number of output columns are not affected by the order and number of options. If no options are specified, each item of information is always displayed in the order of line count, word count, byte count, and file name.
Linux wc command Syntax
wc [option] [file]
The meaning of each option in the command is shown in the table.
Option | Description |
---|---|
-l |
Counting rows |
-w |
Counting the words |
-c |
Counting bytes |
-m |
Counting characters |
-L |
Counting the length of the longest line in the file |
Linux wc command Examples
Common Examples
wc -l * # Count all the lines of files in the current directory and the total number of lines.
wc -l *.js # Count the number of lines and the total number of lines of all files with .js suffix in the current directory.
find . * | xargs wc -l # The number of lines and the total number of lines of all files in the current directory and subdirectories.
Counting the number of lines in file wc -l /home/deepinout/Android-12-Camera-Changes/libcameraservice/CameraService.cpp
wc -l /home/deepinout/Android-12-Camera-Changes/libcameraservice/CameraService.cpp
Output:
Counting the number of words in the file /root/aa
[root@rhel ~]# wc -w /root/aa
8 /root/aa
//Total number of words in file /root/aa is 8
Counting the number of characters in file /root/aa
[root@rhel ~]# wc -m /root/aa
18 /root/aa
//The total number of characters in the file /root/aa is 18, including spaces and newlines, each space is a character and each newline is a character
Counting the number of bytes in file /root/aa
[root@rhel ~]# wc -c /root/aa
22 /root/aa
// The total number of bytes in the file /root/aa is 22, and a Chinese language is represented by three bytes
Statistics of the length of the longest line in file /root/aa
[root@rhel ~]# wc -L /root/aa
8 /root/aa
Stats the number of lines, words and bytes in the file /root/aa
[root@rhel ~]# wc /root/aa
3 8 22 /root/aa
Statistics of how many subdirectories and files are in the /root directory
[root@rhel ~]# ls /root|wc -l
11
// You can see that the total number of subdirectories and files in the /root directory is 11
View the number of bytes, words, and lines in a file
wc test.txt
# Output results
7 8 70 test.txt
# Number of lines, Number of words, Number of bytes, File name
How to print only statistics and not file names with wc command
wc -l < test.txt
# Output results
7
used to count the number of files in the current directory (not including hidden files)
# To remove the TOTAL line
expr $(ls -l | wc -l) - 1
# Output results
8
Counting the number of lines of all files in the current directory and the total number of lines
[root@centos7 ~]# wc -l *
21 LICENSE
270 README.md
wc: example: read: Is a directory
785 lerna-debug.log
25 lerna.json
wc: node_modules: read: Is a directory
23603 package-lock.json
79 package.json
3 renovate.json
24786 total