Linux wc command details

  

Using Linux's wc command combined with other commands to calculate rows and other information. Count with wc under Linux. Returns the number of lines, words, bytes, etc. of the file.

See an example: wc wc1.txt3 5 16 wc1.txt The output information is: line number Words Bytes File name. More specific, a single statistic. Wc -m filename: displays the number of characters in a file wc -l filename: displays the number of lines in a file wc -L filename: displays the length of the longest line in a file wc -w filename: the number of words in a file needs to be noted : Looks like wc statistics is determined by line breaks. That is to say, the last line should have a newline, and the number of lines in the last wc is correct, otherwise there will be one less line. To illustrate this problem, look at a perl test: perl -e 'print "a"'| Wc0 1 1perl -e 'print "a\ "'| Wc1 1 2 In the above example, the print command prints a. If there is no line break \ , then the line does not exist. There are the following points: 1: A Chinese character occupies three bytes (unauthenticated, personally thinks it is two bytes), an invisible character such as a carriage return also occupies a byte 2: the end of a line if not returned The car character is not a line, that is, if there is no line break at the end of the last line of a file, the number of lines counted by the wc command will be one less than the actual number of lines. The actual number of lines is the number of lines you see. In fact, there is no carriage return, it really can't be counted as a line (Note: if it is in an existing text, the default is that all lines have an invisible carriage return) 3: The so-called one word is a continuous character , that is, a continuous letter or Chinese character that is not divided by a null character is counted as a word. For example, the following is a text called abc.txt, which is opened after using vi: (For the sake of detail, we use the arrow ┘ to indicate the carriage return. Actually there is no) You are nice.Right??OK.~~~You are nice.┘Right??┘┘┘OK.┘~~~ Then:wc abc.txt6 5 28 abc.txt ie 6 lines, 5 words, 28 characters. Let's count it. There are 6 lines of invisible carriage returns; there are 5 consecutive letters that are not separated by null characters, namely You, are, nice., Right??, OK. Bytes (or characters) contain carriage returns. A total of 28. For example: example1: How to count the number of files in a directory and the total number of lines of code under linux command to know the total number of files with the specified suffix name: find . -name "*.cpp" |  Wc -l knows the total number of lines of code in a directory and the number of individual file lines: find . -name "*.h" |  Xargs wc -l

example2: Number of files in the linux statistics folder

The first method: ls -l| Grep “^-”| Wc -l

ls -l Long list output file information in this directory (note the files here, different from normal files, may be directories, links, device files, etc.). If ls -lR| Grep “^-”| Wc-l can be counted together with files in subdirectories.

grep ^- Here, the long list output information is filtered, and only the general file is retained. If only the directory is reserved, ^d

wc -l counts the number of rows of the output information because it has been filtered. Only the general file is left, so the statistical result is the number of lines of general file information, and since one line of information corresponds to one file, it is the number of files.

The second method: find ./-type f| Wc -l

Because the default find will go to the subdirectory search, if you only want to find the file in the current directory, use find ./-maxdepth 1 -type f| Wc -l can be.

It should be noted that the second method will be much faster than the first method, especially when counting subdirectories. Example3: Want to know how many users are logged into the system? Who |  Wc -l

Copyright © Windows knowledge All Rights Reserved