Javatpoint Logo
Javatpoint Logo

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.

Introduction to wc

wc is short for word count. It's a command mainly used to count purposes. It shows a four-columnar result by default. The first column displays the number of lines available in the specified file, the second column displays the number of words available in the file, the third column displays the number of characters available in the file, and the fourth column is the name of the file itself which are provided as an argument.

The wc command shows one line of counts for all files, and it shows the file name pursuing the counts if the file was provided as an argument. The wc command shows the final line having the cumulative counts along with the 'total' file name if more than a FILE is provided. The counts are displayed in order, i.e., newlines, words, characters, bytes, and maximum line length.

In a field, all counts are shown right justified with at least a single space among fields; thus, the file names and numbers generally line up in columns nicely. The count field width varies according to the inputs, so we should not rely on a specific field width.

Brief History of wc

Since 1987 issue 2, the wc command has been a component of the X/Open Portability Guide. It was acquired into the initial POSIX.1 version and Single Unix Specification. It occurred in Version 1 Unix. Besides, GNU wc is a component of the GNU textutils package, but it's now a component of GNU coreutils. The wc version grouped in GNU coreutils was given by David MacKenzie and Paul Rubin.

Also, the wc command is a component of the MSX-DOS2 Tools of ASCII for the second version of MSX-DOS2. The command is a Microsoft Windows isolated package as a component of the GnuWin32 project.

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

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:

Linux wc Filters

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:

Linux wc Filters

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:

Linux wc Filters

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:

Linux wc Filters

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:

Linux wc Filters

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:

Linux wc Filters

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:

Linux wc Filters

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:

Linux wc Filters
Next TopicLinux od





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA