wccommand is a part of the coreutils Linux package containing the GNU core utilities. Use
wcto count the number of characters, words, lines, and bytes in a file or standard input.This tutorial will provide details about the
wccommand and its options. The article also includes helpful examples to demonstrate how
wcworks together with other commands.
- A system running Linux.
- Access to the command line/terminal.
Linux wc Command SyntaxThe
wccommand takes the following syntax:
By default, the output shows the number of new lines, words, and bytes in a file, followed by the file name. To view stats for multiple files, list the files in a single command:
wc [options] [location/file]
The output shows the information for each file, followed by the total number of lines, words, and bytes. Use input redirect to stop
wc [options] [location/file1] [location/file2] [location/file3]
wcfrom printing the file name:
Alternatively, use the cat command to list the contents of the file, then pipe the output to
wc < [file/location]
cat [file/location] | wc
Linux wc Command OptionsThe
wccommand takes the following options:
|Print the number of bytes.|
|Print the number of characters.|
|Print the number of lines.|
|Read the input from the files specified by NUL-terminated names in the file. If |
|Print the length of the longest line.|
|Print the number of words.|
|Show version information.|
Linux wc ExamplesThe examples below illustrate the use of the
Use wc with the find CommandUse the find command to provide output for
wc. The example below lists the number of characters for each file in the
/etcfolder whose filename starts with
The output of
find /etc -name '30*' -print0 | wc -m --files0-from=-
findis piped to
wc, which then outputs the relevant stats.
Show Stats for a List of FilesThe
wccommand can read from a file with file names to provide the stats for each file in the list. For
wcto be able to read the file correctly, the names in the file need to be NUL-terminated.
Note: A NUL-terminated string is a string that ends with a null-char, the character whose all bits are zero.
findto create a file containing a NUL-terminated list of files located in the current directory:
The following command reads the file and provides the byte count for each of the files:
find * -print0 > search.txt
wc -c --files0-from=search.txt
Use wc to Count Files and DirectoriesTo find the number of files and directories in the current directory, pipe the ls command to
ls | wc -l
-loption counts the number of lines in the
lsoutput. This number corresponds to the total number of files and directories.
Perform wc Counts Across Multiple FilesUse
wcto count characters, words, lines, and bytes across multiple files. For example, to see the total word count of every TXT file in a directory, type:
cat *.txt | wc -w
catcommand pipes to
wcthe contents of all the TXT files in the directory.
wc -wcounts the total number of words.
Find the Longest Line in All the FilesThe
-Loption prints the length of the longest line for each file. If more than one file is specified, the
totalrow shows the longest line across all files.For example, to find the longest line in all the TXT files in a directory, type:
wc -L *.txt
wcprocesses the TXT files and, for each file, prints the number of characters in the longest line. The last row shows the character count of the longest line in all the files.
ConclusionThis tutorial presented the
wccommand and its options. You also learned how
wcworks in conjunction with other Linux commands.Refer to the Linux Commands Cheat Sheet article for more command examples.