Skip to content

Latest commit

 

History

History
126 lines (82 loc) · 2.28 KB

File metadata and controls

126 lines (82 loc) · 2.28 KB

wc

The wc (word count) command is used to count the number of lines, words, and bytes in files or standard input in a Linux system.

Basic Usage

  • Syntax:

    wc [options] [file...]
  • Examples:

    wc file.txt
    • Displays the number of lines, words, and bytes in file.txt.
    wc -l file.txt
    • Counts only the lines in file.txt.
    echo "Hello World" | wc -w
    • Counts the words in the string "Hello World".
    wc -c file.txt
    • Counts the number of bytes in file.txt.

Common Options

  • -l:

    • Counts the number of lines.
    wc -l file.txt
    • Displays only the line count of file.txt.
  • -w:

    • Counts the number of words.
    wc -w file.txt
    • Displays only the word count of file.txt.
  • -c:

    • Counts the number of bytes.
    wc -c file.txt
    • Displays only the byte count of file.txt.
  • -m:

    • Counts the number of characters.
    wc -m file.txt
    • Displays only the character count of file.txt.
  • -L:

    • Displays the length of the longest line in the file.
    wc -L file.txt
    • Shows the length of the longest line in file.txt.
  • --help:

    • Displays help information about the wc command.
    wc --help
    • Shows usage information and options.

Quick Tips

  • Combining Options:

    • Combine options to get multiple counts in one command.
    wc -l -w file.txt
    • Displays both the line and word count of file.txt.
  • Processing Multiple Files:

    • You can pass multiple files to wc to get counts for each file and a total.
    wc file1.txt file2.txt
    • Displays counts for both file1.txt and file2.txt, followed by a total.
  • Using Piping:

    • wc can be combined with other commands using pipes to process output.
    ls -l | wc -l
    • Counts the number of lines of output from ls -l, effectively counting the number of items in a directory.

Summary

The wc command is a simple but versatile tool for counting lines, words, bytes, and characters in files or input streams. It’s particularly useful for analyzing text files, processing command output, and performing quick text-based data analysis.