# wc — Count Lines, Words and Bytes

> Practical guide to wc: count lines, words, characters and bytes in files or pipes — from a quick wc -l to the longest line.

Source: https://www.jpkc.com/db/en/cheatsheets/files-text/wc/

<!-- PROSE:intro -->
wc (word count) counts lines, words, characters and bytes – whether in files or in a pipe. Most often you reach for `wc -l` to count lines, for example to tally log entries, matches or files. Pass several files and wc appends a total line automatically. Worth knowing: `-c` counts bytes while `-m` counts characters (so with UTF-8 an umlaut is correctly one character), and using `< file` instead of an argument suppresses the filename in the output. This guide shows you the most useful variants for everyday work.
<!-- PROSE:intro:end -->

## Basic Usage

`wc <file>` — Show line count, word count, and byte count of a file.

```bash
wc README.md
```

`wc <file1> <file2>` — Show counts for multiple files with a total summary line.

```bash
wc src/*.js
```

`<command> | wc` — Count lines, words, and bytes from piped input.

```bash
cat access.log | wc
```

## Line Count

`wc -l <file>` — Count the number of lines in a file.

```bash
wc -l access.log
```

`wc -l <file1> <file2>` — Count lines in multiple files with a total.

```bash
wc -l *.csv
```

`<command> | wc -l` — Count the number of lines in command output.

```bash
grep 'ERROR' app.log | wc -l
```

`wc -l < <file>` — Count lines without printing the filename (redirect input).

```bash
wc -l < data.csv
```

## Word Count

`wc -w <file>` — Count the number of words in a file.

```bash
wc -w essay.txt
```

`wc -w <file1> <file2>` — Count words in multiple files with a total.

```bash
wc -w *.md
```

`<command> | wc -w` — Count words in command output.

```bash
echo 'hello world foo bar' | wc -w
```

## Character & Byte Count

`wc -c <file>` — Count the number of bytes in a file.

```bash
wc -c image.png
```

`wc -m <file>` — Count the number of characters (multibyte-aware, respects locale).

```bash
wc -m utf8-text.txt
```

`wc -c < <file>` — Get the file size in bytes without the filename.

```bash
wc -c < archive.tar.gz
```

## Longest Line

`wc -L <file>` — Show the length of the longest line in a file.

```bash
wc -L config.json
```

`wc -L <file1> <file2>` — Show the longest line length for each file with a maximum.

```bash
wc -L *.py
```

`<command> | wc -L` — Find the length of the longest line in command output.

```bash
cat /etc/hosts | wc -L
```

## Combining Flags

`wc -lw <file>` — Show both line count and word count.

```bash
wc -lw README.md
```

`wc -lc <file>` — Show line count and byte count.

```bash
wc -lc access.log
```

`wc -lwm <file>` — Show lines, words, and characters (multibyte-aware).

```bash
wc -lwm article.txt
```

## Pipelines & Practical Patterns

`find <path> -name '<glob>' | wc -l` — Count how many files match a pattern.

```bash
find src/ -name '*.js' | wc -l
```

`find <path> -name '<glob>' -exec cat {} + | wc -l` — Count total lines of code across all matching files.

```bash
find src/ -name '*.py' -exec cat {} + | wc -l
```

`git diff --name-only | wc -l` — Count the number of changed files in a git working tree.

`git log --oneline | wc -l` — Count the total number of commits in a git repository.

`ps aux | wc -l` — Count the number of running processes (subtract 1 for the header).

`ls -1 <path> | wc -l` — Count the number of files and directories in a folder.

```bash
ls -1 /var/log/ | wc -l
```

`grep -c '' <file>` — Alternative line count using grep (equivalent to wc -l).

```bash
grep -c '' data.csv
```

`wc -l <files> | sort -rn | head -<n>` — Find the largest files by line count.

```bash
wc -l src/*.js | sort -rn | head -10
```

`wc -l <files> | tail -1` — Get only the total line count across multiple files.

```bash
wc -l *.log | tail -1
```

`awk 'END {print NR}' <file>` — Alternative line count using awk (useful in larger awk scripts).

```bash
awk 'END {print NR}' access.log
```

<!-- PROSE:outro -->
## Conclusion

wc is the fastest tool for a sense of size and scope and sits at the end of countless pipelines. Mind the subtle difference between `-c` (bytes) and `-m` (characters): for plain ASCII the two are equal, but for UTF-8 text with umlauts or emoji they diverge. Strictly speaking `wc -l` counts newline characters – if a file ends without a trailing newline, the last line is not counted. And if you use input redirection `wc -l < file` instead of `wc -l file`, you get just the bare number without a filename, which is handy to reuse in scripts.

## Further Reading

- [GNU Coreutils manual: wc](https://www.gnu.org/software/coreutils/manual/html_node/wc-invocation.html) – complete reference for every option
- [man7.org: wc(1)](https://man7.org/linux/man-pages/man1/wc.1.html) – the Linux manual page
<!-- PROSE:outro:end -->

## Related Commands

- [sort](https://www.jpkc.com/db/en/cheatsheets/files-text/sort/) – sort lines
- [uniq](https://www.jpkc.com/db/en/cheatsheets/files-text/uniq/) – filter and count duplicate lines
- [grep](https://www.jpkc.com/db/en/cheatsheets/files-text/grep/) – search lines by pattern (also counts with -c)

