Bash Wiki
Posts (Latest 30 updated) :
Read all
Contents:
  1. [Linux] Bash wc Usage: Count lines, words, and characters in files
    1. Overview
    2. Usage
    3. Common Options
    4. Common Examples
    5. Tips

[Linux] Bash wc Usage: Count lines, words, and characters in files

Overview

The wc (word count) command in Bash is a utility that allows users to count the number of lines, words, and characters in a file or standard input. It is a simple yet powerful tool for analyzing text data.

Usage

The basic syntax of the wc command is as follows:

wc [options] [arguments]

Common Options

  • -l: Count the number of lines.
  • -w: Count the number of words.
  • -c: Count the number of bytes (characters).
  • -m: Count the number of characters (useful for multibyte characters).
  • -L: Print the length of the longest line.

Common Examples

  1. Count lines in a file:
    wc -l filename.txt
    
  2. Count words in a file:
    wc -w filename.txt
    
  3. Count characters in a file:
    wc -c filename.txt
    
  4. Count lines, words, and characters in a file:
    wc filename.txt
    
  5. Count the longest line in a file:
    wc -L filename.txt
    
  6. Count lines from standard input:
    echo -e "Hello\nWorld" | wc -l
    

Tips

  • Combine options to get multiple counts at once. For example, wc -lw filename.txt will give you both line and word counts.
  • Use wc in combination with other commands through pipes to analyze output. For instance, grep "pattern" filename.txt | wc -l will count how many lines contain a specific pattern.
  • Remember that wc can also read from standard input, making it versatile for quick counts without needing to specify a file.