Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

80%
+6 −0
Q&A How to count the lines of a file?

I think the typical way to do this uses wc ("word count") with the -l ("lines") option. $ wc -l /path/to/file 47 /path/to/file $ wc -l </path/to/file 47 $ cat /path/to/file | wc -l ...

posted 3mo ago by Michael‭  ·  edited 1mo ago by Michael‭

Answer
#5: Post edited by user avatar Michael‭ · 2024-04-11T21:05:19Z (about 1 month ago)
Newlines
  • I think the typical way to do this uses [`wc`][wc] ("word count"?) and the `-l` option.
  • ```sh
  • $ wc -l /path/to/file
  • 47 /path/to/file
  • $ wc -l </path/to/file
  • 47
  • $ cat /path/to/file | wc -l
  • 47
  • ```
  • `wc` with no options prints the number of lines, words, and bytes, but `-l` can limit it to just lines.
  • [wc]: https://linux.die.net/man/1/wc
  • I think the typical way to do this uses [`wc`][wc] ("word count") with the `-l` ("lines") option.
  • ```sh
  • $ wc -l /path/to/file
  • 47 /path/to/file
  • $ wc -l </path/to/file
  • 47
  • $ cat /path/to/file | wc -l
  • 47
  • ```
  • `wc` with no options prints the number of lines, words, and bytes, but `-l` can limit it to just lines. As a comment notes: "lines" is the number of newline characters, so you may be off-by-one if you don't have a trailing newline.
  • [wc]: https://linux.die.net/man/1/wc
#4: Post edited by user avatar Michael‭ · 2024-02-15T20:14:14Z (3 months ago)
Different run styles
  • I think the typical way to do this uses [`wc`][wc] ("word count"?) and the `-l` option.
  • ```sh
  • wc -l /path/to/file
  • ```
  • `wc` with no options prints the number of lines, words, and bytes, but `-l` can limit it to just lines.
  • [wc]: https://linux.die.net/man/1/wc
  • I think the typical way to do this uses [`wc`][wc] ("word count"?) and the `-l` option.
  • ```sh
  • $ wc -l /path/to/file
  • 47 /path/to/file
  • $ wc -l </path/to/file
  • 47
  • $ cat /path/to/file | wc -l
  • 47
  • ```
  • `wc` with no options prints the number of lines, words, and bytes, but `-l` can limit it to just lines.
  • [wc]: https://linux.die.net/man/1/wc
#3: Post edited by user avatar matthewsnyder‭ · 2024-02-15T20:00:02Z (3 months ago)
wc supports filename arguments, and prints the filename as well.
  • I think the typical way to do this uses [`wc`][wc] ("word count"?) and the `-l` option.
  • ```sh
  • wc -l </path/to/file
  • ```
  • `wc` with no options prints the number of lines, words, and bytes, but `-l` can limit it to just lines.
  • [wc]: https://linux.die.net/man/1/wc
  • I think the typical way to do this uses [`wc`][wc] ("word count"?) and the `-l` option.
  • ```sh
  • wc -l /path/to/file
  • ```
  • `wc` with no options prints the number of lines, words, and bytes, but `-l` can limit it to just lines.
  • [wc]: https://linux.die.net/man/1/wc
#2: Post edited by user avatar r~~‭ · 2024-02-14T16:34:16Z (3 months ago)
cat isn't needed
  • I think the typical way to do this uses [`wc`][wc] ("word count"?) and the `-l` option.
  • ```sh
  • cat /path/to/file | wc -l
  • ```
  • `wc` with no options prints the number of lines, words, and bytes, but `-l` can limit it to just lines.
  • [wc]: https://linux.die.net/man/1/wc
  • I think the typical way to do this uses [`wc`][wc] ("word count"?) and the `-l` option.
  • ```sh
  • wc -l </path/to/file
  • ```
  • `wc` with no options prints the number of lines, words, and bytes, but `-l` can limit it to just lines.
  • [wc]: https://linux.die.net/man/1/wc
#1: Initial revision by user avatar Michael‭ · 2024-02-14T14:31:18Z (3 months ago)
I think the typical way to do this uses [`wc`][wc] ("word count"?) and the `-l` option.

```sh
cat /path/to/file | wc -l
```

`wc` with no options prints the number of lines, words, and bytes, but `-l` can limit it to just lines.

[wc]: https://linux.die.net/man/1/wc