Linux wc command

Linux wc command

Linux wc Command – Count Words, Lines, and Characters

The wc (word count) command in Linux is used to count the number of lines, words, characters, and bytes in a file or input. It is often used in text processing, scripting, and log analysis.

Syntax

wc [OPTIONS] [FILE]

If no file is specified, wc reads from standard input.

Basic Usage of wc

1. Count Lines in a File

To count the number of lines in a file:

wc -l filename

Example:

wc -l example.txt

Output:

42 example.txt

This means example.txt contains 42 lines.

2. Count Words in a File

To count the number of words:

wc -w filename

Example:

wc -w example.txt

Output:

300 example.txt

This means example.txt contains 300 words.

3. Count Characters in a File

To count characters (including spaces and newlines):

wc -m filename

Example:

wc -m example.txt

Output:

1500 example.txt

This means the file has 1500 characters.

4. Count Bytes in a File

To count the file size in bytes:

wc -c filename

Example:

wc -c example.txt

Output:

1800 example.txt

This means the file is 1800 bytes in size.

5. Display Line, Word, and Character Count Together

Running wc without options shows lines, words, and bytes:

wc filename

Example:

wc example.txt

Output:

42 300 1800 example.txt

This represents:

  • 42 → Lines
  • 300 → Words
  • 1800 → Bytes

6. Count Lines, Words, and Characters from Input (Piped Data)

You can use wc with other commands.

Count the number of lines in a command output

ls -1 | wc -l

This counts the number of files in the current directory.

Count words from input

echo "Linux is awesome" | wc -w

Output:

3

Example: Count Lines in Multiple Files

wc -l file1.txt file2.txt

Output:

10 file1.txt 20 file2.txt 30 total

It shows the count for each file and the total.

Difference Between wc -m and wc -c

OptionMeaning
-mCounts characters (useful for Unicode files).
-cCounts bytes (file size).

Conclusion

The wc command is useful for analyzing text files and command outputs. It is often combined with other tools like grep, awk, and sed for powerful text processing.

Would you like additional details or SEO optimization? 🚀

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close