Linux wc Command
Linux wc command helps in counting the lines, words, and characters in a file. It displays the number of lines, number of characters, and the number of words in a file. Mostly, it is used with pipes for counting operation.
Syntax:
Options:
Some useful command line options supported by the wc command are as following:
-c, –bytes: It is used to print the byte counts.
-m, –chars: It is used to print the character counts.
-l, –lines: It is used to print the newline counts.
–files0-from=F: It is used to read input from specified files.
-L, –max-line-length: It is used to print the maximum display width.
-w, –words: It is used to print the word counts.
–help: It is used to display the help manual.
–version: It is used to display the version information.
Examples of the wc Command
Let’s see the following examples of the wc command:
- Display count information of a file
- Display count information of multiple files
- Display the number of lines in a file
- Display the number of characters in a file
- Display the number of bytes in a file
- Display the number of words in a file
- Count the number files in a directory
- Display the length of the longest line
Display count information of a file
To display the complete count information of a file, execute the command without any argument. It will display the number of lines, words, and bytes from the file. Execute the command as follows:
Consider the below command:
The above command will display the number of lines, number of words, number of bytes, and file name from the file ‘exm.txt’. Consider the below output:
Display count information of multiple files
To display the complete count information of multiple files at once, specify the file names after space (‘ ‘). It is executed as follows:
Consider the below example:
The above command will display the number of words, the number of characters, and the number of the bytes from the files ‘exm.txt’ and ‘marks.txt’. Consider the below output:
Display the number of lines in a file
The ‘-l’ option is used to display the number of lines in a file. It is executed as follows:
Consider the below example:
The above command will display the number of lines from ‘exm.txt’. Consider the below output:
Display the number of characters in a file
The ‘-m’ option is used to display the number of characters in a file. It is executed as follows:
Consider the below example:
The above command will display the number of words from the file ‘exm.txt’. Consider the below output:
Display the number of bytes in a file
The ‘-c’ option is used to display the number of bytes in a file. It is executed as follows:
Consider the below example:
The above command will display the number of bytes in a file. Consider the below output:
Display the number of words in a file
The ‘-w’ option is used to display the total number of words from a file. It is executed as follows:
Consider the below example:
The above command will display the total number of words from the file ‘exm.txt’. Consider the below output:
Count the number files in a directory
To count the number of files and folders in a directory, combine the wc command with the ls command. Execute it as follows:
The above command will display the count of the files from the current working directory. Consider the below output:
Display the length of the longest line
The ‘-L’ option is used to display the length of the longest line from a file. It is executed as follows:
Consider the below example:
The above command will display the length of the longest line of the file ‘exm.txt’. Consider the below output: