LinuxCommandLibrary

wc

Count words, lines, characters, or bytes

TLDR

Count all lines in a file

$ wc [[-l|--lines]] [path/to/file]
copy

Count all words in a file
$ wc [[-w|--words]] [path/to/file]
copy

Count all bytes in a file
$ wc [[-c|--bytes]] [path/to/file]
copy

Count all characters in a file (taking multi-byte characters into account)
$ wc [[-m|--chars]] [path/to/file]
copy

Count all lines, words and bytes from stdin
$ [find .] | wc
copy

Count the length of the longest line in number of characters
$ wc [[-L|--max-line-length]] [path/to/file]
copy

SYNOPSIS

wc [OPTION]... [FILE]...

PARAMETERS

-c, --bytes
    Print the byte counts.

-m, --chars
    Print the character counts.

-l, --lines
    Print the newline counts.

-w, --words
    Print the word counts.

-L, --max-line-length
    Print the length of the longest line.

--files0-from=F
    Read input from the files specified by NUL-terminated names in file F; If F is -, then read names from standard input.

--help
    Display help message and exit.

--version
    Output version information and exit.

DESCRIPTION

The wc command is a Unix/Linux utility used to count the number of bytes, words, and lines in a file or from standard input. It provides a quick and easy way to get basic statistics about text files. By default, wc displays the line count, word count, and byte count, separated by spaces, followed by the filename. If multiple files are provided as arguments, wc will output the counts for each file individually, followed by a total count for all files combined. The command's flexibility lies in its options, which allow users to select specific counts (lines, words, bytes, or characters) or modify the output format. It's an essential tool for scripting, data analysis, and general file information retrieval. The utility works by reading through the input stream/file, keeping track of line breaks, whitespaces and bytes. It can be useful for quick overview of the content or in scripting for validation and filtering of outputs. Without options, it displays the line, word, and byte counts, followed by the filename. If no filename is specified, wc reads from standard input, and no filename is displayed in the output. Its presence on nearly all *nix systems makes it a reliable tool across different environments.

CAVEATS

When counting characters using -m, wc correctly handles multi-byte characters in UTF-8 encoded files. However, if the input file is not UTF-8 encoded, the character count might not accurately reflect the number of visual characters.

EXIT STATUS

The wc command returns an exit status of 0 on success, and a non-zero value if an error occurs, such as being unable to open or read a file.

STANDARD INPUT

If no file arguments are given, wc reads from standard input. This allows you to pipe the output of other commands to wc for counting purposes.
For example: ls -l | wc -l counts the number of files/directories.

HISTORY

The wc command is a standard utility in Unix-like operating systems, dating back to the early days of Unix. Its initial implementation was simple and focused on providing basic file statistics. Over time, the command has been included in POSIX standards, ensuring its availability across different Unix versions. The options and functionalities have remained largely consistent, making it a reliable and ubiquitous tool for system administrators and developers.
It is now part of GNU coreutils package.

SEE ALSO

head(1), tail(1), grep(1), sed(1), awk(1)

Copied to clipboard