Linux wc command - count the number of lines, words, bytes and characters in a file

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:

Linux wc command

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
Like(0)

Related

Linux Login Logout Command
Linux login commandLinux logout commandLinux nologin commandLinux exit commandLinux sulogin commandLinux rlogin commandLinux poweroff commandLinux ctrlaltdel CommandLinux shutdown commandLinux halt commandLinux reboot commandLinux init commandLinux runlevel commandLinux telinit command
Linux File Management Command
Linux cat commandLinux tac commandLinux nl commandLinux more commandLinux less commandLinux head commandLinux tail commandLinux rev commandLinux fold commandLinux fmt commandLinux expand commandLinux pr commandLinux sort commandLinux uniq commandLinux cut commandLinux comm commandLinux diff commandLinux join commandLinux diff3 commandLinux cmp commandLinux colrm commandLinux paste commandLinux mkdir commandLinux tr commandLinux split commandLinux csplit commandLinux tee commandLinux unexpand commandLinux patch commandLinux awk commandLinux sed commandLinux od commandLinux pwd commandLinux cd commandLinux ls commandLinux dir commandLinux dirs commandLinux touch commandLinux rmdir commandLinux cp commandLinux mv commandLinux rm commandLinux install commandLinux tmpwatch commandLinux file commandLinux du commandLinux wc commandLinux tree commandLinux cksum commandLinux md5sum commandLinux sum commandLinux dirname commandLinux mkfifo Command
Cron Expressions
Cron Expression to Run Every Day at 12 PMUnderstanding Vue Cron ExpressionsUnderstanding JS Cron ExpressionsA Comprehensive Guide to Cron Expressions for Scheduled TasksUnderstanding Linux Cron ExpressionsUnderstanding Quartz Cron ExpressionsCron ExpressionCron Time ExpressionCron Expression ParsingCron Expression: Executing a Task Every SecondCron Expression for Every Minute ExecutionCron Expression to Execute Every 10 MinutesCron Expression: Executing Every HourCron Expression to Execute Once a YearCron Expression: How to Schedule a Task to Run Daily at Midnight?