Skip to content
Linux Basics: Distinguish Bytes, Characters, and Width with GNU wc

Linux Basics: Distinguish Bytes, Characters, and Width with GNU wc

October 30, 2026

A UTF-8 text file can have different byte, character, and display-width counts. GNU wc exposes each measure separately. Choosing the right one prevents confusing a storage limit with a text length or terminal layout limit.

Prepare a scratch directory

Tested on Linux with GNU Bash 5.2.37 and GNU coreutils 9.7. You need Bash, a text editor, and the utilities shown below. The script creates and removes its own temporary fixture directory.

Keep the same terminal open. Stop if setup fails, before saving the source:

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save this as demonstrate-gnu-wc-bytes-characters-width.sh:

#!/usr/bin/env bash
set -euo pipefail
export LC_ALL=C
fixture_dir="$(mktemp -d)"
trap 'rm -rf -- "$fixture_dir"' EXIT
cd -- "$fixture_dir"
export LC_ALL=C.utf8
printf 'caf\303\251\nA\tB\nlast' > sample.txt
printf 'Bytes: '
wc -c < sample.txt
printf 'Characters: '
wc -m < sample.txt
printf 'Newlines: '
wc -l < sample.txt
printf 'Maximum display width: '
wc -L < sample.txt
test "$(wc -c < sample.txt)" -eq 14
test "$(wc -m < sample.txt)" -eq 13
test "$(wc -l < sample.txt)" -eq 2
test "$(wc -L < sample.txt)" -eq 9

Run it:

bash demonstrate-gnu-wc-bytes-characters-width.sh

Observed output:

Bytes: 14
Characters: 13
Newlines: 2
Maximum display width: 9

Read the result

The é in café takes two UTF-8 bytes but counts as one character. Newline and tab characters also contribute to -m. The file has two newline bytes; the final last has no newline, so -l reports two. For -L, the tab after A advances to the next eight-column tab stop, and B occupies column nine. Input redirection avoids printing a filename next to each count.

Apply it carefully

The character and width measures depend on the locale. This example requires the installed C.utf8 locale; check locale -a if your system uses another UTF-8 locale name. A character count is not a count of user-perceived grapheme clusters: combining marks can contribute separately. Display width is not a browser layout measurement, and complex emoji or terminal behavior can differ from wc’s calculation. Use -c for byte quotas and a Unicode-aware application library when a product requirement concerns visible characters.

The command reference documents the available options.

Clean up

Remove the saved script, return to the starting directory, and remove the empty practice directory:

rm -- demonstrate-gnu-wc-bytes-characters-width.sh
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

If the directory is not empty, inspect what remains rather than deleting unknown files.

Acknowledgements

This post was created with assistance from AI.

Last updated on