Table of content

WC command in Linux

The wc command reads either standard input or a list of files and generates newline count, word count, and byte count.

Syntax :

wc <option> <filename>

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.
Display count information of a file

Execute the wc command without any argument, it will display the number of lines, words, and bytes from the file

wc <file name>  

Output :

1 4 17 f1
  • 1 is the number of lines.
  • 4 is the number of words.
  • 17 is the number of characters
Display count information of multiple files

To display the complete count information of multiple files give the below command

wc <file1> <file2> 
Display the number of lines in a file using the l option

The '-l' option is used to print the total number of lines in a file

wc - l <file name>  
Display the number of characters in a file using the m option

The '-m' option is used to print the total number of characters in the file.

wc -m <file name>
Display the number of bytes in a file using the c option

The '-c' option is used to display the total number of bytes in a file.

wc -c <file name>  
Display the number of words in a file using the w option

The '-w' option is used to display the total number of words from a file. It is executed as follows:

wc -w <file name>  
Count the number of files in a directory using the l option

To count the total number of files in a directory give the below command,

ls | wc -l  
Display the length of the longest line using the L option

The '-L' option is used to display the length of the longest line from a file

wc -L <file name>  

Output :
wc-command-output-linux

The output of wc --help command :
wc-command-linux

To count the number of occurrences of a particular word using grep and wc command

To count the number of occurrences of a particular word using grep and wc command in a text file give the below command

Syntax:

grep -o -i <search word> <file name> | wc -l

Example :

grep -o -i Hi! f1 | wc -l

Output :
wc-output-linux

The -ooption used with the grep command gives an output when the given text is matched in the file and passes the output to the wc-lcommand which counts the number of times the provided text is matched in the file.

0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions