Oneliner: Count How Many Times a String Appears in a File

When working with text files, it is usefull to know how to count how many times a string appears in a file. To do that, use this oneliner:

Watch Free Movies

$ grep -o string /path/to/file | wc -l

$ cat jedi
jedi jedi jedi jedi jedi
$ grep -o jedi /path/to/file | wc -l

grep -o jedi matches only the jedi strings and displays one match per line. wc -l counts the lines only.

Scroll to Top