Know more about files using Linux wc commandIn Linux, wc is a small command line utility that can be used to display details like number of words, newlines, bytes etc for a file. The wc command can display some extra information like length of the longest line in the file. In this article we will discuss the usage of this command through examples. Here is the basic syntax of wc command : wc [OPTION]... [FILE]...
As clear from the syntax, multiple files can be supplied as input to the Linux wc command.
Linux wc command examples
The file input.txt will be used in the examples that follow.
Here are the contents of input.txt : # cat input.txt Hi, Welcome to Linux. Have fun. Thanks. 1. A basic exampleIn its basic form, wc command displays the following output :# wc input.txt
4 7 40 input.txt
In the output above:
2. Print bytes count with -c option
The option -c can be used to print the number of bytes in the file.
Here is an example : # wc -c input.txt 40 input.txtSo as we can see, number of bytes (40 in this case) were printed in the output. 3. Print number of newline with -l option
The option -l can be used to print the number of newlines in the file.
Here is an example : # wc -l input.txt 4 input.txtSo as we can see, the output displays number of newlines ( 4 in this case). 4. Print length of longest line with -L option
The option -L can be used to print the length of longest line in the file.
Here is an example : # wc -L input.txt 17 input.txtSo we see that the output displays the length of the longest line (17 in this case). 5. Print number of words with -w option
The option -w can be used to print the number of words in a file.
Here is an example : # wc -w input.txt 7 input.txtSo we can see that the output displays the number of words in the file (7 in this case). 6. Multiple files as input
The wc command can accept multiple files as input. In this case a extra column 'total' is also produced in the output.
Here is an example : # wc input.txt input.txt 4 7 40 input.txt 4 7 40 input.txt 8 14 80 totalSo we see that the details for individual files were produced in the output. Also last row containing totals of each column was produced in output. 7. Use stdin to give input file name with --files0-from option
The character - can be used to provide the name of the file from stdin.
Here is an example : # wc --files0-from=- input.txt4 7 40 input.txt ^C
So we see that the wc command waited till it was provided input file name through stdin. Once the name was provided and Ctrl+d was pressed twice, the output appeared in the same line. After this, the wc command went again in the waiting mode to accept another input file from command line.
Also if instead of - if a file name is given as input to the option --files0-from then in that case the file names mentioned in that file are accepted as input file names. For more information on wc, visit the man page of wc. |